Question

I'm working on a iOS 7 application, which will have a feature to bookmark articles. I want to display a text message (e. g. a label) once a user bookmarks an article. The text message should then automatically disappear after, for example, 2 seconds. How can I do that? UIAlertView is not what I'm looking for.

Was it helpful?

Solution

If you're refering to some kind of toast in Android. There is no such thing like that in iOS, you'd have to implement it yourself or use a library like Ratikanta Patra mentionned it.

You could try a snippet like that, and adjusting it according to your needs.

UIView *toast = [[UIView alloc] initWithFrame:CGRectMake(40, 200, 240, 40)];
toast.backgroundColor = [UIColor blackColor];
[self.view addSubview:toast];
toast.alpha = 0;

[UIView animateWithDuration:0.4 animations:^{
    toast.alpha = 1;
} completion:^(BOOL finished) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.4 animations:^{
            toast.alpha = 0;
        } completion:^(BOOL finished) {
            [toast removeFromSuperview];
        }];
    });
}];

OTHER TIPS

In this case a toast message would be the ideal solution. I prefer using iToast. here is the link: https://github.com/ecstasy2/toast-notifications-ios

As per the website:

You can add it anywhere on the screen depending of the importance of the iToast. We display them with a gravity of Top when it is of medium importance, Center when it is Very importan an Bottom when it is of low importance.

Create your label, customize it as you want, add it to the screen and hide it :

UILabel *notification = [[UILabel alloc] init];
notification.text = ...
notification.frame = ...
notification.hidden = YES;
[self.view addSubView:notification];

Then, when you need to show it, and hide it after delay :

notification.hidden = NO;
[UIView animateWithDuration:2
                      delay:2
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{
                     notification.hidden = YES;
                 }
                 completion:nil];

Note that thehidden property will not animate. Use notification.alpha if you want an animation. I used animateWithDuration: simply because it has an easy delay parameter.

You can use a timer to count 2 seconds after it appear. And after 2 seconds hide your label.

SKTipAlertView - https://github.com/SaKKo/SKTipAlertView

Hope this helps.

Though this is a library to show tips, but it would work perfectly in your case with little modification: https://github.com/chrismiles/CMPopTipView

On the contrary, you can use your own UITextview and NSTimer class to create your own custom control. Use the following function to trigger your timer when you make the view visible

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(autoDismissAnimatedDidFire:) userInfo:userInfo repeats:NO]; This will call the autoDismissAnimatedDidFire function after 2.0 sec. In this function, hide the view again

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top