如何使UIViewanimation序列重复和自动反向
如何使这个复杂的animation重复和自动反向? 有没有什么办法可以添加选项UIViewAnimationOptionAutoreverse | UIViewAnimationOption重复到这个animation序列?
[UIView animateWithDuration:1.0f animations:^{ someView.frame = someFrame1; } completion:^(BOOL finished) { [UIView animateWithDuration:0.5f animations:^{ someView.frame = someFrame2; } completion:nil]; }];
从点1到2到3到2到1的animation并重复,可以在iOS 7和更高版本中使用animateKeyframesWithDuration
:
someView.frame = frame1; [UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{ [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{ someView.frame = frame2; }]; [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{ someView.frame = frame3; }]; } completion:nil];
如果使用自动布局,则可以dynamic改变约束常量:
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{ [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{ topConstraint.constant = 200; leftConstraint.constant = 200; [self.view layoutIfNeeded]; }]; [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{ topConstraint.constant = 100; leftConstraint.constant = 300; [self.view layoutIfNeeded]; }]; } completion:nil];
或者,使用自动布局的方法是停用约束,然后可以使用frame
值或您有什么animation。
在iOS的早期版本中,您可以使用CAKeyframeAnimation
来沿着path设置animation:
UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(100.0, 100.0)]; [path addLineToPoint:CGPointMake(200.0, 200.0)]; [path addLineToPoint:CGPointMake(100.0, 300.0)]; CAKeyframeAnimation *animatePosition = [CAKeyframeAnimation animationWithKeyPath:@"position"]; animatePosition.path = [path CGPath]; animatePosition.duration = 1.0; animatePosition.autoreverses = YES; animatePosition.repeatCount = HUGE_VALF; [self.someView.layer addAnimation:animatePosition forKey:@"position"];
你可以用尽可能多的点来做到这一点。 如果您想要沿着弯曲的path(例如圆形或贝塞尔曲线)进行animation处理,这也是非常有用的技巧。
要在两点之间进行animation处理,可以使用animateWithDuration:delay:options:animations:completion:
例如:
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut animations:^{ // do whatever animation you want, eg, someView.frame = someFrame1; } completion:NULL];
这将someView
的运动从起始帧animation到someFrame1
并返回。
顺便说一下,使用UIViewAnimationOptionCurveEaseInOut
与UIViewAnimationOptionAutoreverse
和UIViewAnimationOptionRepeat
相结合将给你一个更平滑的效果,animation反转和重复。
UIImageView *pin = [UIImageView new]; [pin setFrame:CGRectMake(115, 160, 30, 60)]; [pin setBackgroundColor:[UIColor redColor]]; [holderView addSubview:pin]; [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{ // [pin setTransform:CGAffineTransformMakeTranslation(0, 20)]; [pin setFrame:CGRectMake(115, 200, 60, 100)]; }completion:^(BOOL finished){ }];
- UITapGestureRecognizer initWithTarget:动作:方法采取参数?
- 架构arm64的未定义符号:“_ OBJC_CLASS _ $ _ Twitter”,
- Objective-C用({…})声明variables
- 添加到手机上的主屏幕
- Swift JSON错误:无法将types'__NSDictionaryM'的值转换为'NSArray'
- 如何在JavaScript中查询iTunessearchAPI?
- 如何从ObjectiveC中的NSString拆分换行符
- embedded在UIPageViewController(ScrollStyle)中的UITableView不能被挖掘 – didSelectRowAtIndexPath没有被触发
- NSDateFormatter- stringFromDate不返回AM / PM