Objective-C Block stops UIGestureRecognizer - iphone

Here is my problem.
I use blocks to display two labels on screen one by one (one READY label appears, then disappears, and GO! label appears, then disappears).
I also have a gesture recognizer to detect if the user is dragging a view.
When my app is displaying the labels, gesture recognizers stop calling their callback.
Here is my code:
[UIView animateWithDuration:1 animations:^{
readyLabel.alpha = 0;
}completion:^(BOOL finished){
[readyLabel removeFromSuperview];
[self.view addSubview:goLabel];
[UIView animateWithDuration:1 animations:^{
goLabel.alpha = 0;
}completion:^(BOOL finished){
self.ball = [[Ball alloc] init];
[self.view addSubview:self.ball];
_timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(moveBall:) userInfo:nil repeats:YES];
}];
}];
What I tried so far is using NSThread to execute my blocks outside of the main thread, but without result.
I could use performSelector:withObject:afterDelay to avoid the problem for my labels (display one label after the first animation has finished) but I think it's a bit dirty.
Why does my gesture recognizer stop calling his callback? Are blocks responsible for this?

This is because block animations disable user interaction. You should use animateWithDuration:delay:options:animations:completion: and specify UIViewAnimationOptionAllowUserInteraction in options.

This is because, when UIView is animating using blocks it prevents UI interaction. To avoid this behaviour you must use the UIViewAnimationOptionAllowUserInteraction option:
[UIView animateWithDuration:1
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
readyLabel.alpha = 0;
}
completion:^(BOOL finished){
[UIView animateWithDuration:1
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
goLabel.alpha = 0;
}
completion:^(BOOL finished){
self.ball = [[Ball alloc] init];
[self.view addSubview:self.ball];
_timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(moveBall:) userInfo:nil repeats:YES];
}
];
}
];

Related

Start animate UIView before complete?

I'm using [UIView animateWithDuration:...] for animate sequence of UIImageView views. Like this:
[UIView animateWithDuration:1.0 animations:^{
imageView.frame = newImageRectPosition;
}completion:^(BOOL finished){
//animate next UIImageView
}];
I need animate 'next UIImageView' not on completion. I need animate 'next UIImageView' on the middle of previous animation, not on completion. Is it possible to do so?
You can setup two UIView animation blocks, one which has a delay of half the duration of the first animation:
[UIView animateWithDuration:1.0
animations:^{ ... }
completion:^(BOOL finished){ ... }
];
[UIView animateWithDuration:1.0
delay:0.5
options:UIViewAnimationCurveLinear
animations:^{ ... }
completion:^(BOOL finished) { ... }
];
There are many options you can use to achieve the effect you're after. One which comes to mind is the use of timers.
Use an NSTimer with a firing interval half of that of your animation, and get the timer to fire off another animation. As long as the two animations don't interfere with each other, you should be ok.
An example would be as so:
NSTimer* timer;
// Modify to your uses if so required (i.e. repeating, more than 2 animations etc...)
timer = [NSTimer scheduledTimerWithTimeInterval:animationTime/2 target:self selector:#selector(runAnimation) userInfo:nil repeats:NO];
[UIView animateWithDuration:animationTime animations:^{
imageView.frame = newImageRectPosition;
} completion:nil];
- (void)runAnimation
{
// 2nd animation required
[UIView animateWithDuration:animationTime animations:^{
imageView.frame = newImageRectPosition;
} completion:nil];
}
With a timer, this could scale up if you needed to do more than two animations, and it all holds together if you need to change the animation time later on.

Fade animation when scrolling horizontal table view

I have a horizontal table view and want to change images each 5 secs. I want to change images with fade animation so the old image fades out and the new one fades in. So i call this method:
self.slideTimer = [NSTimer scheduledTimerWithTimeInterval:5
target:self
selector:#selector(slideToNextImage)
userInfo:nil
repeats:YES];
And here is my slideToNextImage:
self.lastIndexPath = indexPath;
[UIView beginAnimations:#"FadeAnimations" context:nil];
[UIView setAnimationDuration:2];
self.horizontalView.alpha = 0.1f;
[self.horizontalView.tableView scrollToRowAtIndexPath:self.lastIndexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:NO];
[UIView commitAnimations];
[UIView beginAnimations:#"FadeAnimations" context:nil];
[UIView setAnimationDuration:2];
self.horizontalView.alpha = 1.0f;
[UIView commitAnimations];
With my realisation the image fades too fast and i see the second image scrolling with no fade animation
the second animation starts without waiting for the first to end.
Try something like this instead:
[UIView animateWithDuration:2.0 animations:^{
self.horizontalView.alpha = 0.1f;
[self.horizontalView.tableView scrollToRowAtIndexPath:self.lastIndexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:NO];
} completion:^(BOOL finished) {
[UIView animateWithDuration:2 animations:^{
self.horizontalView.alpha = 1.0f;
}];
}];

Erratic behavior with NSTimers and NSRunLoops

I am attempting to have a UIImageView (a picture of a finger) animate from right to left, using CGAffineTransformMakeTranslation(). This animation will repeat until the user performs a swipe, moving the user along in a tutorial. All of this is already working swimmingly as follows:
[UIView animateWithDuration:1.0 animations:^ {
self.finger.alpha = 1.0;
}completion:^(BOOL finished) {
CGAffineTransform originalTransform = self.finger.transform;
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^ {
self.finger.transform = CGAffineTransformMakeTranslation(-200, 0);
self.finger.alpha = 0.0;
}completion:^(BOOL finished) {
self.finger.transform = originalTransform;
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(repeatSwipeAnimation1) userInfo:nil repeats:YES];
self.swipeTimerForFinger1 = timer;
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:self.swipeTimerForFinger1 forMode:NSDefaultRunLoopMode];
[self.swipeTimerForFinger1 fire];
}];
}];
And the selector:
-(void)repeatSwipeAnimation1 {
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^ {
self.finger.alpha = 1.0;
}completion:^(BOOL finished) {
CGAffineTransform originalTransform = self.finger.transform;
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^ {
self.finger.transform = CGAffineTransformMakeTranslation(-200, 0);
self.finger.alpha = 0.0;
}completion:^(BOOL finished) {
self.finger.transform = originalTransform;
}];
}];
}
The finger animates and translates beautifully.
The issue comes when I want to do this with a different finger with a different timer. I have the same exact code, however it is a different finger and different selector for the timer.
What happens is that the timer's selector will not translate the UIImageView, and (more scary) the timer will not invalidate when I call the method to invalidate. Upon debugging, I see that the the 2nd timer is calling the 2nd selector, but just not behaving (e.g. not translating, and fading in the 2nd finger too rapidly).
What I am assuming is that I need to somehow turn off the NSRunLoop when I first call it? This is the first time working with NSRunLoop so I apologize for my ignorance. Any help is very much appricated.
Well, you certainly have a block retain cycle going on. You need to use either the __block or __weak specifier. See Blocks and Variables. Your problem may be related to a memory issue.
Make sure you invalidate the first timer when it is done.
For safety, you may also want to reset the transform on your UIImageView before attempting to transform it. You can do that like so:
finger.transform = CGAffineTransformIdentity;

In iOS4, having an NSTimer running with an NSRunLoop disables Touch/Gesture Input

This is a followup from a previous question (which was resolved beautifully by Michael Frederick).
I am currently debugging between a iOS5 device and an iOS4 device. Essentially I have a UIImage which is animating a swipe to instruct the user to swipe to continue. The animation code is as follows:
[UIView animateWithDuration:1.0 animations:^ {
self.finger.alpha = 1.0;
}completion:^(BOOL finished) {
CGAffineTransform originalTransform = self.finger.transform;
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^ {
self.finger.transform = CGAffineTransformMakeTranslation(-200, 0);
self.finger.alpha = 0.0;
}completion:^(BOOL finished) {
self.finger.transform = originalTransform;
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(repeatSwipeAnimation1) userInfo:nil repeats:YES];
self.swipeTimerForFinger1 = timer;
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:self.swipeTimerForFinger1 forMode:NSDefaultRunLoopMode];
[self.swipeTimerForFinger1 fire];
}];
}];
and the timer selector:
-(void)repeatSwipeAnimation1 {
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^ {
self.finger.alpha = 1.0;
}completion:^(BOOL finished) {
CGAffineTransform originalTransform = self.finger.transform;
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^ {
self.finger.transform = CGAffineTransformMakeTranslation(-200, 0);
self.finger.alpha = 0.0;
}completion:^(BOOL finished) {
self.finger.transform = originalTransform;
}];
}];
}
This works completely fine. However, on iOS4, swipe gestures seem to be disabled; I am unable to swipe to continue as I am able to on iOS5. The only theory I have is that having the timer run on the main thread somehow disables the gesture recognizer (which I presume is sensing touch on the main thread as well). My only issue is that I have attempted to put this animation in the background as follows:
[UIView animateWithDuration:1.0 animations:^ {
self.finger.alpha = 1.0;
}completion:^(BOOL finished) {
CGAffineTransform originalTransform = self.finger.transform;
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^ {
self.finger.transform = CGAffineTransformMakeTranslation(-200, 0);
self.finger.alpha = 0.0;
}completion:^(BOOL finished) {
self.finger.transform = originalTransform;
[NSThread detachNewThreadSelector:#selector(goToNewThread) toTarget:self withObject:nil];
}];
}];
and the new thread selector:
-(void)goToNewThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(repeatSwipeAnimation1) userInfo:nil repeats:YES];
self.swipeTimerForFinger1 = timer;
[[NSRunLoop currentRunLoop] addTimer:self.swipeTimerForFinger1 forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
[self.swipeTimerForFinger1 fire];
[pool release];
}
The animation is in place, but yet again, it seems to disable any swipe gesture recognition. I have mulled over SO, my O'Reilly materials, and the Apple documentation and have not been able to find a solution. Thank you for your time.
To get the formality out of the way; You need to add UIViewAnimationOptionAllowUserInteraction to the options in your animation block.
To answer your comment question. Yes you can rather easily use more than one animation option. You generally use the bitwise or operator "|", like so:
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction animations...
NJones had the correct answer. I simply needed to add UiViewAnimationOptionAllowUserInteraction under the options of my [UIView animationWithDuration...] code.

iPhone loading view "slide effect"

I would to achieve that when you request something in my app (like a button pressed or whatever) I would like it to appear a small rectangle that says "loading.." and when the loading is completed it disappear with a "slide up effect". I've made a simple mockups for describing well what I'm trying to achieve. Do you have any hint for me? Thanks.
NOTE: I don't want pull to refresh effect (the loading view appears without the screen scrolling, it appears for example when you send a comment)
UPDATE:
I've tried the Cirrostratus code:
- (IBAction)displayLoadingScreen:(id)sender
{
NSLog(#"pressed");
UIView *loadingView = [[UIView alloc] init];
loadingView.frame = CGRectMake(0,-40,320,40);
[loadingView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:loadingView];
//animate in within some method called when loading starts
[UIView animateWithDuration:1.0
animations:^{
loadingView.frame = CGRectMake(0,0,320,40);
}
completion:^(BOOL finished) {
}];
for (int i = 0; i < 10000; i++) {
NSLog(#"%d", i);
}
//animate out within some method called when loading ends
[UIView animateWithDuration:1.0
animations:^{
loadingView.frame = CGRectMake(0,-40,320,40);
}
completion:^(BOOL finished){
}];
}
Why the loading view slide in after the for loop? If you try it, before it prints the for loop and after executes the two animations. Ideas?
UPDATE 2
- (void)stopLoading {
[UIView animateWithDuration:1.0
animations:^{
loadingView.frame = CGRectMake(0,-40,320,40);
}
completion:^(BOOL finished){
}];
}
- (void)startLoading {
loadingView.frame = CGRectMake(0,-40,320,40);
[loadingView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:loadingView];
//animate in within some method called when loading starts
[UIView animateWithDuration:1.0
animations:^{
loadingView.frame = CGRectMake(0,0,320,40);
}
completion:^(BOOL finished) {
}];
sleep(5);
[self stopLoading];
}
- (IBAction)displayLoadingScreen:(id)sender
{
NSLog(#"button pressed");
[self startLoading];
}
loadingView.frame = CGRectMake(0,-40,320,40);
[loadingView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:loadingView];
//animate in within some method called when loading starts
[UIView animateWithDuration:1.0
animations:^{
loadingView.frame = CGRectMake(0,0,320,40);
}
completion:^(BOOL finished){
}];
//animate out within some method called when loading ends
[UIView animateWithDuration:1.0
animations:^{
loadingView.frame = CGRectMake(0,-40,320,40);
}
completion:^(BOOL finished){
}];
What I would do is add a view in front of your other views with the loading design you want.
Then when the loading is finished (use a notification or just call a function of the view), slide it up with that kind of animation :
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
//change the frame to make the view go out of the screen
}
completion:^(BOOL finished){
//do whatever you want with the view (release it for instance)
}];
you can do it with the help of spinner else you take just view and set animation with its coordinate when you click on any object it will popup n your view and after completion of loading it you have to set '-' coordinate(frame) for your popup view hop it will hekp you......