一个处理程序重复的UIView animateWithDuration
2016-01-11 15:27
411 查看
一个处理程序重复的UIView animateWithDuration?
我的UIView animateWithDuration的重复我的观点动画。我怎样才可以有一个处理程序,可以在以后停止这个动画?例如,重复的动画开始的,我需要从以后停止-------------------------------------------------------------------------------
1. 你可以做这样的假设你已经创建了一个取消的财产。作为块的startAnimation调用记录需要被包裹在一个异步调用,以避免堆栈溢出.a定要与任何类类型你确实有更换“身份证”。
- (void)startAnimation { [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^(void) { //animate } completion:^(BOOL finished) { if(!self.canceled) { __weak id weakSelf = self; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [weakSelf startAnimation]; }]; } } ]; }
2. 动画的目的是要反复进行动画图象的反弹。当没有担心手动停止它,那么你只需要设置三个属性
(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat)和用于移动图像动画块代码-
self.image.center = CGPointMake(self.image.center.x, self.image.center.y+25);下面是动画的完整的代码:
[UIView animateWithDuration:0.5 delay:0 options:( UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction) animations:^{self.image.center = CGPointMake(self.image.center.x, self.image.center.y+25);} completion:nil];
就是这样。但是如果你需要一个手动控制,然后额外的代码是必需的。首先,根据jaminguy,你需要有一个布尔属性指示回路/停止(self.playAnimationForImage)的动画,并用干净的动画代码 CodeGo.net,会从其他地方调用。这里是
-(void)animateImageBounce{ [UIView animateWithDuration:0.5 delay:0 options:( UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction) animations:^{self.image.center = CGPointMake(self.image.center.x, self.image.center.y+25);} completion:^(BOOL finished){if (finished && self.playAnimationForImage){ self.image.center = CGPointMake(self.image.center.x, self.image.center.y-25); [self animateImageBounce]; }}];
这里是从动画呼叫开始
-(void)someMethod{ ... self.playAnimationForFingers = YES; [self animateImageBounce];
} 我想说明的一点是,在手动控制,需要重新设置图像的center.y右后方之前,未来的递归调用是
3. 事实上,在递归调用的解决方案并没有制定出动画开始古怪的行为:每隔2-3 animatation重复周期我得到动画rest。项目的优先部分反弹后(移动项下)的第二部分(动起来)的执行几乎瞬间。我的事情是必须做的递归调用。 因此,我认为。该解决方案将是启动动画与自动翻转并重复选择和,块,以检查是否有标志(
self.playAnimationForFingers)表示,以停止动画。
-(void)animateFingersForLevelCompleteScreen{ //fix: reset the place of bouncing fingers (starting place). //Otherwise the fingers will slowly move to the bottom at every level. //This resetting is not working when placed inside UIView //animate complete event block self.image.center = CGPointMake(10 + self.image.frame.size.width/2, 95 + self.image.frame.size.height/2); [UIView animateWithDuration:0.5 delay:0 options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction) animations:^{ self.image.center = CGPointMake(self.image.center.x, self.image.center.y+25); } completion:^(BOOL finished){ /*finished not approapriate: finished will not be TRUE if animation was interrupted. It is very likely to happen because animation repeats && */ if (!self.playAnimationForFingers){ [UIView setAnimationRepeatAutoreverses:NO]; } }]; }
4. u能CABasicAnimation代替。
CABasicAnimation *appDeleteShakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; appDeleteShakeAnimation.autoreverses = YES; appDeleteShakeAnimation.repeatDuration = HUGE_VALF; appDeleteShakeAnimation.duration = 0.2; appDeleteShakeAnimation.fromValue = [NSNumber numberWithFloat:-degreeToRadian(5)]; appDeleteShakeAnimation.toValue=[NSNumber numberWithFloat:degreeToRadian(5)]; [self.layer addAnimation:appDeleteShakeAnimation forKey:@"appDeleteShakeAnimation"];
然后当u想停止它,你可以调用
[self.layer removeAnimationForKey:@"appDeleteShakeAnimation"];
相关文章推荐
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 一个处理程序重复的UIView animateWithDuration?
- Android开发笔记(四十四)动态UI事件
- unixODBC without the GUI
- 动画 UIView animateWithDuration 使用详解
- Fail to decode request due to: RpcInvocation
- YTU 2900: F-A Simple Question
- iOS开发之UICollectionView(二)
- 5.6. Looping Techniques(循环技术)
- iOS之UINavigationController的使用
- Android Can only use lower 16 bits for requestCode
- UITabBarItem
- UIBarButtonItem
- DbUils的数据库操作之版本更新
- 【转载】MQTT学习笔记——MQTT协议体验 Mosquitto安装和使用
- (UIPickerView)选择器的使用--相互依赖的多列选择器
- Performance Tuning guide 翻译 || Performance Tuning Guide 11G中新增特性
- 85.如何更改iOS上的UISearchBar组件的内部背景UITextField颜色 修改UISearchBar删除按钮ClearButton
- UIImage size压缩后图片下面出现白色线问题
- UILabel 的行间距