How to modify the animationduration rate of a UIView in xcode with a button
I'm hoping someone can help. I have a fairly standard UIView animation where the only real animation is a CGAffineTransformMakeScale that is set on a loop. I want to have two buttons, one that increases the rate (or more accurately reduces the animation duration) of scale and one that decreases the rate (or more accurately increases the animation duration) of the scale.
Is this even possible? I apologise if this is obvious but I am a novice and looking for advice - even if it's directed reading. Let me know if I should supply any further info to help.
Many thank开发者_JS百科s in advance!
I don't know of a way to modify an in-progress animations.
Here's code that I think does what you want. It uses the increase/decrease buttons to change an ivar animationDuration
, and manually loops the animation as two halves (a forward half and a reverse half). Each time the new animation (forwards or reverse) starts, it gets the value for animationDuration
at that point in time, so for short animations it will appear to change pretty much instantly. It would not work well for long duration animations however, as it actually only changes the animation speed at the max/min points of the animation.
You could break the animation up even smaller if you wanted to update more frequently - e.g. split it into 4 instead of 2 (1 -> 1.5, 1.5 -> 2.0, 2.0 -> 1.5, 1.5 -> 1.0) or even more if you need it.
Header (MyViewController.h):
// Define some constants to be edited to suit our needs
#define kAnimationDurationMin 0.1
#define kAnimationDurationDefault 0.4
#define kAnimationDurationMax 2.0
#define kAnimationDurationStep 0.05
@interface MyViewController : UIViewController {
// The variable to store the target animation duration in
double animationDuration;
// Whether the next animation should be a reverse animation or not
BOOL reverse;
// The view to be animated, this should be connected in Interface Builder
IBOutlet UIView *ball;
}
//The method to actually do the animation
-(void)doAnimation;
// These 2 methods should be connected to the 'touch up inside' event of the relevant buttons in Interface Builder
- (IBAction)incButtonPressed:(id)sender;
- (IBAction)decButtonPressed:(id)sender;
@end
Implementation (MyViewController.m):
#import "MyViewController.h"
@implementation MyViewController
-(void)viewDidLoad {
// If animation duration has not yet been set (it will be zero) then set it to the default.
if (animationDuration < kAnimationDurationMin) animationDuration = kAnimationDurationDefault;
// Start the animation
[self doAnimation];
}
// This method does the animation
-(void)doAnimation {
[UIView beginAnimations:@"ball" context:NULL];
[UIView setAnimationDuration: animationDuration];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
// Do not repeat
[UIView setAnimationRepeatCount: 0];
// Not autoreversing allows us to trigger the animationDuration change twice as frequently.
[UIView setAnimationRepeatAutoreverses:NO];
// When the animation is complete, start it again by calling [self doAnimation];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(doAnimation)];
if (reverse) {
// Reset to default
ball.transform = CGAffineTransformMakeScale(1,1);
} else {
// Target of forward animation
ball.transform = CGAffineTransformMakeScale(2,2);
}
// Toggle reverse
reverse = !reverse;
[UIView commitAnimations];
}
- (IBAction)incButtonPressed:(id)sender {
animationDuration += kAnimationDurationStep;
if (animationDuration > kAnimationDurationMax) animationDuration = kAnimationDurationMax;
}
- (IBAction)decButtonPressed:(id)sender {
animationDuration -= kAnimationDurationStep;
if (animationDuration < kAnimationDurationMin) animationDuration = kAnimationDurationMin;
}
@end
You want setAnimationDuration of the UIView class:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
view.transform = CGAffineTransformMakeScale(0.5, 0.5);
[UIView commitAnimations];
Here's the relevant documentation:
https://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW62
精彩评论