I'm trying to convert working UIView animations to using blocks. Aside from the completion callback, I don't see what's different about them. Could someone clarify something I might be missing?
This works as expected
[UIView beginAnimations:#"Curl" context:nil];
[UIView setAnimationDuration:.15];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.storyTextView cache:YES];
self.storyTextView.text = text;
[UIView commitAnimations];
This does change the page's text, but no animation is show, it's just an instant transition.
UIViewAnimationOptions curl = UIViewAnimationOptionTransitionCurlUp;
[UIView animateWithDuration:.15 delay:0 options:(UIViewAnimationOptionCurveEaseInOut | curl) animations:^{
self.storyTextView.text = text;
} completion:^(BOOL finished) {
if (finished){
// pass
}
}];
Moreover, setting delay in the blocks style animation does nothing to affect the instant transition, it just runs the completion block after the delay.
You'll want to try UIView's transitionWithView instead:
UIViewAnimationOptions curl = UIViewAnimationOptionTransitionCurlUp;
[UIView transitionWithView:self.view duration:0.15 options:(UIViewAnimationOptionCurveEaseInOut | curl) animations:^{
self.storyTextView.text = text;
} completion:nil];
Related
I added in the button click event:
[UIView beginAnimations:nil context:nil];
sender.titleLabel.font = [UIFont systemFontOfSize:18];
[UIView commitAnimations];
The animation is not working. But if I remove [UIView commitAnimations];, the animation works. Why?
If I don't add [UIView commitAnimations];, what will happen?
Why you are not using this for view animations?
[UIView animateWithDuration:1.0 animations:^{
// place your animations code here
}];
Note: please visit this url and see the section What can be animated. Only few properties can be used for animations
https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html
Try this, it will help. In the example below, viewObject may be label in your case.
[UIView animateWithDuration:1.0 animations:^{
viewObject.transform = CGAffineTransformMakeScale(1.5, 1.5);
}];
I have the following code
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
mainPage.frame=CGRectMake(0, -[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height,[UIScreen mainScreen].bounds.size.width);
} completion:nil];
Is there any way to control the animation duration of the subviews of mainPage.
You can use UIView:animateKeyframesWithDuration then use UIView:addKeyframeWithRelativeStartTime to set its start time and duration
[UIView animateKeyframesWithDuration:2 delay:0 options:0 animations:^{
[UIView addKeyframeWithRelativeStartTime:0.32 relativeDuration:0.68 animations:^{
view.frame = frame;
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:1 animations:^{
subview.frame = frame2;
}];
} completion:^(BOOL finished) {
}];
If you want the subviews to animate with a different duration then you'll need to animate each of them separately. You can loop through the mainPage.subviews array and issue an animateWithDuration:delay:options:animations:completion: call to each subview in turn, each with a different duration.
I new to IOS programming.
I want to toggle button border color automatically at start of apps to get user attention,
I have tried the below code, but only the final color is selected.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:0.0];
[UIView setAnimationDuration:3.0];
button.layer.borderWidth=2.5f;
button.layer.borderColor=[[UIColor blueColor]CGColor];
[UIView setAnimationDelay:3.0];
button.layer.borderColor=[[UIColor clearColor]CGColor];
[UIView setAnimationDelay:6.0];
button.layer.borderColor=[[UIColor redColor]CGColor];
[UIView commitAnimations];
}
What you're doing is an invalid way to chain animations. As a result, only the last changes are applied. Additionally, you should be using block based animations, which Apple has recommended since iOS 4. Should be something like this:
[UIView animateWithDuration:3.0 animations:^{
button.layer.borderWidth=2.5f;
button.layer.borderColor=[[UIColor blueColor]CGColor];
} completion:^(BOOL finished) {
[UIView animateWithDuration:3.0 animations:^{
button.layer.borderColor=[[UIColor clearColor]CGColor];
} completion:^(BOOL finished) {
[UIView animateWithDuration:3.0 animations:^{
button.layer.borderColor=[[UIColor redColor]CGColor];
}];
}];
}];
This answer is the same as 0x7fffffff's answer, except it uses block variables so it looks a little cleaner and hopefully makes more sense:
void (^animateToRed)(BOOL finished) = ^(BOOL finished) {
[UIView animateWithDuration:3.0 animations:^{
button.layer.borderColor=[[UIColor redColor]CGColor];
} completion: nil];
}
void (^animateToClear)(BOOL finished) = ^(BOOL finished) {
[UIView animateWithDuration:3.0 animations:^{
button.layer.borderColor=[[UIColor clearColor]CGColor];
} completion:animateToRed];
}
[UIView animateWithDuration:3.0 animations:^{
button.layer.borderWidth=2.5f;
button.layer.borderColor=[[UIColor blueColor]CGColor];
} completion:animateToClear];
UIView's animateWithDuration:animations:completion: method is the best way to animate changes over time.
It takes 3 arguments.
A duration as a CGFloat, which is a measure of the length of the animation in seconds.
An animation block, which tells what animations to perform.
A completion block, which allows you to execute code after the animation is complete.
This code snippet creates two completion blocks.
The animateToRed completion block handles the animating of the border to red. It's completion block is nil, at this point, we're done animating.
The animateToClear completion block handles the animating of the border to clear. It's completion block is the animateToRed which we just defined.
Finally, we call animateWithDuration, animating the border to blue, and passing the animateToClear block for the completion (which in turn calls the animateToRed block).
For an animation this simple and with no repeated animations, it may seem like slightly overkill to do it this way (though it is slightly more readable). However, with a more complicated series of animations, especially if there's any repetitiveness, creating block variables like this to use and pass quickly becomes quite helpful.
I have this code to simulate a multiple camera flash
[UIView animateWithDuration:0.1
delay:0.f
options:UIViewAnimationOptionAutoreverse
animations:^{
[UIView setAnimationRepeatCount:2];
flash.alpha=1.f;
}
completion:^(BOOL finished) {
flash.alpha = 0;
}];
flash is a white UIImageView (full screen) that starts with alpha = 0.
If you try to use this code, you will notice that at the end flash remains full white for a little time and it's not perfect for my effects, what can I do to solve this?
The problem with your code is you do autoreverse using the option UIViewAnimationOptionAutoreverse, while also specifying your own final state in the completion block.
Try this:
[UIView animateWithDuration:0.1f
delay:0
options:UIViewAnimationOptionAutoreverse
animations:^{
[UIView setAnimationRepeatCount:2];
flash.alpha=1.f;
}
completion:nil];
I tried animating a sequence of UIViews from a mutable array to simulate this animation
for(int k = 0; k< [imageViewCarrier count] ; k++){
UIView *transformingView = [imageViewCarrier objectAtIndex:k];
[UIView animateWithDuration:30.0 animations:^{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:30.0];
[UIView setAnimationDelegate:self];
[UIView transitionFromView:transformingView toView:splicedImageView duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
[UIView commitAnimations];
}completion:^(BOOL finished){
NSLog(#"Transition done");
}];
}
The animation seems to be too fast. Any suggestions on this. Did search some documentation but couldn't figure it out. Some help would be greatly appreciated!
Hm, I think you might be doing it the wrong way. If memory serves me, when using [UIView animinateWithDuration:animiations:completion:], you shouldn't call [UIView beginAnimations:nil context:NULL]; and [UIView commitAnimations]; or setAnimationDelegate: or setAnimationDuration: for that matter, since they are the old way of animating views that you had to use before the block-based methods were introduced.
I'd try leaving those out and see what happens. Also, note that the duration parameter is in seconds, so 30.0 seems a bit too long.
And just a style note: the "proper" Objective-C way to iterate through a collection is as follows:
for(UIView* view in imageViewCarrier){
[view doSomething];
}