前言:
我负责努力,其余交给运气...
正文:
今天遇到了一个诡异的问题:产品要求呢,点击cell的时候,要做一个选中色然后3秒内渐变回归正常的效果。于是呢,因为用到的地方有点多,所以我就在cell中添加touchesBegan,这样:
/**点击cell变色,三秒渐隐*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
self.backgroundColor=RGB(255,251,230);
[UIView animateWithDuration:3 animations:^{
self.backgroundColor = [UIColor clearColor];
}];
}
结果万万没想到的是:因为cell中有UI控件,然后有点击事件,结果动画进行时,所有的点击事件都无效了...第一时间想到的就是isUserInteractionEnabled,结果怎么设置都不好使。查了一下isUserInteractionEnabled的文档才知道,里面有这么一段:
- During an animation, user interactions are temporarily disabled for all views involved in the animation, regardless of the value in this property. You can disable this behavior by specifying the allowUserInteraction option when configuring the animation.
大意就是:在动画期间,无论此属性中的值如何,都会临时禁用动画中涉及的所有视图的用户交互。但是可以设置animation的option为allowUserInteraction而禁用此行为。(百度翻译+半懵半猜...)
然后,就找到了这个方法:
[UIView animateWithDuration: delay: options: animations: completion:^(BOOL finished) {}]
查了一下options的选项:
UIViewAnimationOptionLayoutSubviews //提交动画的时候布局子控件,表示子控件将和父控件一同动画。
UIViewAnimationOptionAllowUserInteraction //动画时允许用户交流,比如触摸
UIViewAnimationOptionBeginFromCurrentState //从当前状态开始动画
UIViewAnimationOptionRepeat //动画无限重复
UIViewAnimationOptionAutoreverse //执行动画回路,前提是设置动画无限重复**
UIViewAnimationOptionOverrideInheritedDuration //忽略外层动画嵌套的执行时间
UIViewAnimationOptionOverrideInheritedCurve //忽略外层动画嵌套的时间变化曲线
UIViewAnimationOptionAllowAnimatedContent //通过改变属性和重绘实现动画效果,如果key没有提交动画将使用快照
UIViewAnimationOptionShowHideTransitionViews //用显隐的方式替代添加移除图层的动画效果
UIViewAnimationOptionOverrideInheritedOptions //忽略嵌套继承的选项
//时间函数曲线相关
UIViewAnimationOptionCurveEaseInOut //时间曲线函数,缓入缓出,中间快
UIViewAnimationOptionCurveEaseIn //时间曲线函数,由慢到特别快(缓入快出)
UIViewAnimationOptionCurveEaseOut //时间曲线函数,由快到慢(快入缓出)
UIViewAnimationOptionCurveLinear //时间曲线函数,匀速
//转场动画相关的
UIViewAnimationOptionTransitionNone //无转场动画
UIViewAnimationOptionTransitionFlipFromLeft //转场从左翻转
UIViewAnimationOptionTransitionFlipFromRight //转场从右翻转
UIViewAnimationOptionTransitionCurlUp //上卷转场
UIViewAnimationOptionTransitionCurlDown //下卷转场
UIViewAnimationOptionTransitionCrossDissolve //转场交叉消失
UIViewAnimationOptionTransitionFlipFromTop //转场从上翻转
UIViewAnimationOptionTransitionFlipFromBottom //转场从下翻转
所以,解决方案就出来了:
/**点击cell变色,三秒渐隐*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
self.backgroundColor=RGB(255,251,230);
[UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.backgroundColor = [UIColor clearColor];
}completion:^(BOOLfinished) {
}];
}
补充:
关于转场动画,它一般是用在下面这个方法中的:
[UIView transitionFromView: toView: duration: options: completion:^(BOOL finished) {}];
该方法效果是从一个view to 另一个view,期间可以使用一些转场动画效果。
总结:
bug就像象牙塔,我们卡在那的时候,感觉无比的草蛋。但是解决后,回望一下,原来如此简单...