UIView animatewithDuration - Remove Animation - ios

I have a recursive UIView animatewith duration: delegate to run an animation. When the view disappears I''m supposed to stop this animation. [self.view.layer removeAllAnimation] doesn't seem to help. ANy other means to stop animation?

What about using the method [UIView animateWithDuration:animations:completion:] ?
You can check inside the completion block if the view has disappeared. If it hasn't - run one more animation iteration.

This solution i came across is based on the UIObject. I added the UIObject to an NSMutableArray on starting the animation. When I leave that view call for [NSMutableArray * removeObject]; inside viewDidDisappear.

Related

Exclude view from UIAnimation

I have made a "pull-to-refresh" and when finished updating the inset is removed using animation block. However I'm getting unwanted animations in the custom uitableviewcell, see movie:
https://www.youtube.com/watch?v=rZcEBm_fTUc&feature=youtu.be
Watch carefully at the bottom. I found this:
How can I exclude a piece of code inside a core animation block from being animated?
But don't know how to use it, here is the code that I'm using:
id animation = ^{
//Remove the contentinset
[self.tableView setContentInset:refreshView.initialInset];
};
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionBeginFromCurrentState
animations:animation
completion:nil];
[refreshView endAnimation];
}
Thankful for any help on this matter!
I was not reusing my UITableViewCells correctly. Before i removed all subviews from the cell and added them again, which is the wrong way to do it.
By using "reuseIdentifier" for my different UITableViewCells and only instantiating the UILabels and UIButtons once the problem disappeared (along with a lot of other problems)

UIView Animation Fails

So, I want to do some basic animations of labels and later views.
I have a label, I'm trying to get it to move when a view loads, so I call the following method at the end of viewDidLoad:
- (void)animateView {
NSLog(#"animateView");
[UIView animateWithDuration:20 animations:^{
// set new position of label which it will animate to
self.dcFirstRunDaysLabel.frame = CGRectMake(20,320,280,215);
}];
}
Instead of animating, the label appears in position.
I've tried every tutorial and read through the docs. I get no errors.
Any thoughts?
Cheers.
Try calling your animateView method in viewDidAppear. Because in viewDidLoad your view isn't visible yet.
viewDidLoad:
Called after the controller’s view is loaded into memory.
viewDidAppear:
Notifies the view controller that its view was added to a view hierarchy.

Animate MapView around screen

I have this animation block:
[UIView animateWithDuration:10 animations:^{
CGPoint centerLeft;
centerLeft.x = self.leftMapPane.center.x - 100;
centerLeft.y = self.leftMapPane.center.y;
CGRect leftMove = CGRectMake(self.leftMapPane.frame.origin.x - 100, self.leftMapPane.frame.origin.x, self.leftMapPane.frame.size.width, self.leftMapPane.frame.size.height);
[self.leftMapPane setFrame:leftMove];
[self.leftMapPane setCenter:centerLeft];
}];
but for some reason although it gets inside the animation block, it doesn't move the mapViews I have around on the screen.
It should be noted that I'm not talking about moving the map but the view that contains the map, aka the mapview.
Any thoughts?
The animation doesn't work in viewDidLoad because at that point the views have been loaded into memory but have not been necessarily added to the view hierarchy. So when you try to animate the views it won't work.
Instead, put your animation call in viewDidAppear and it should work. This way you're sure the views you're animating are in the view hierarchy. I tried this in a sample project using your animation code and it worked fine.

fade in/fade out UISegmentedControl in XIB

I have a UISegmentedControl set up in my XIB. I want it to appear on viewDidLoad and if the user taps the area of the screen it's in, and then to disappear if the user taps it again or to fade out if the user leaves it alone.
In looking around for how to manage this I've found a lot of stuff about fading UIViews, but not as much on fading individual subviews, and little at all on fading elements in the XIB. I tried to adapt the UIView stuff but failed.
How can I make this work?
EDIT: Okay, I've got the appearance at viewDidLoad and the fade out working. But when the user taps the area where the UISegmentedControl is (now invisible because alpha=0), nothing happens. This is the code I'm using:
- (IBAction)tapInvisibleSegContr
//This is connected to the UISegmentedControl with the action Touch Up Inside. Until now, the segmented control has been at alpha=0 since fading after viewDidLoad.
{
self.segContrAsOutlet.alpha=1.0;
[self fadeMethodThatWorksInViewDidLoad];
NSLog(#"Yup, tapped.");
}
I'm not even getting the NSLog. I've got the action hooked up to the UISegmentedControl, with the action Touch Up Inside. What am I missing?
If it is resident in a xib, just put his alpha to 0, do the properly connections: an Outlet and an IBAction for value changed
Then in the viwDidLoad right after [super viewDidLoad] write:
[UIView animateWithDuration:1 animations:^{self.mySegOutlet.alpha = 1;}];
Inside the IBAction right after you code the answer before the last } write:
[UIView animateWithDuration:1 animations:^{self.mySegOutlet.alpha = 0;}];
This is the easiest method.
Bye
In the xib set your control's alpha to 0.0, then use UIView animation methods to animate its alpha to 1.0. For example:
[UIView animateWithDuration:1.0 animations:^{
self.segmentedControl.alpha = 1.0f;
}];
EDIT: To your problem with not getting the action called, try attaching it for the value changed control event - I don't think UISegmentedControl sends for touch up inside.

Selecting second tab stops animation in first tab and does not restart

I create a new tabbed application project in Xcode 4.2 using Storyboards.
In viewDidLoad, I add the following code:
[UIView animateWithDuration:30
delay:0
options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear
animations:^(void) {
CGRect imageViewFrame = self.scrollingImageView.frame;
imageViewFrame.origin.x = CGRectGetWidth(imageViewFrame) / -2;
self.scrollingImageView.frame = imageViewFrame;
} completion:nil];
Then I create the associated UIImageView property and wire it up in IB.
#property (nonatomic, strong) IBOutlet UIImageView *scrollingImageView;
I put an image into the imageview, run it in the simulator or my iPod Touch, and I see scrolling.
However, when I select the second tab and then go back to the first tab, the animation completes what appears to be immediately and I cannot get it to restart, even if I put the above into viewWillAppear. I have searched through lots of answers and cannot solve it.
Any help would be appreciated.
It appears that the issue you are dealing with is specific to tabbed bar controllers, not storyboards or the specific animation. The issue is that when you switch to a different tab, the animation stops, and when you return to the first tab, it doesn't resume.
Ordering the animation again in viewWillAppear does not work (as you mentioned), but if you first remove the animation in viewWillDisappear, then ordering it again in viewWillAppear should work. For example
- (void)viewWillDisappear:(BOOL)animated
{
[self.view.layer removeAllAnimations]; // or whatever you need to use to remove your specific animation
}
NB: The animation removal code should be in viewWillDisappear rather than in viewWillUnload, since viewWillUnload does not get called when you switch tabs.

Resources