前言:
我負責努力,其余交給運氣...
正文:
今天遇到了一個詭異的問題:產(chǎn)品要求呢,點擊cell的時候,要做一個選中色然后3秒內(nèi)漸變回歸正常的效果。于是呢,因為用到的地方有點多,所以我就在cell中添加touchesBegan,這樣:
/**點擊cell變色,三秒漸隱*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
self.backgroundColor=RGB(255,251,230);
[UIView animateWithDuration:3 animations:^{
self.backgroundColor = [UIColor clearColor];
}];
}
結(jié)果萬萬沒想到的是:因為cell中有UI控件,然后有點擊事件,結(jié)果動畫進行時,所有的點擊事件都無效了...第一時間想到的就是isUserInteractionEnabled,結(jié)果怎么設(shè)置都不好使。查了一下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.
大意就是:在動畫期間,無論此屬性中的值如何,都會臨時禁用動畫中涉及的所有視圖的用戶交互。但是可以設(shè)置animation的option為allowUserInteraction而禁用此行為。(百度翻譯+半懵半猜...)
然后,就找到了這個方法:
[UIView animateWithDuration: delay: options: animations: completion:^(BOOL finished) {}]
查了一下options的選項:
UIViewAnimationOptionLayoutSubviews //提交動畫的時候布局子控件,表示子控件將和父控件一同動畫。
UIViewAnimationOptionAllowUserInteraction //動畫時允許用戶交流,比如觸摸
UIViewAnimationOptionBeginFromCurrentState //從當前狀態(tài)開始動畫
UIViewAnimationOptionRepeat //動畫無限重復
UIViewAnimationOptionAutoreverse //執(zhí)行動畫回路,前提是設(shè)置動畫無限重復**
UIViewAnimationOptionOverrideInheritedDuration //忽略外層動畫嵌套的執(zhí)行時間
UIViewAnimationOptionOverrideInheritedCurve //忽略外層動畫嵌套的時間變化曲線
UIViewAnimationOptionAllowAnimatedContent //通過改變屬性和重繪實現(xiàn)動畫效果,如果key沒有提交動畫將使用快照
UIViewAnimationOptionShowHideTransitionViews //用顯隱的方式替代添加移除圖層的動畫效果
UIViewAnimationOptionOverrideInheritedOptions //忽略嵌套繼承的選項
//時間函數(shù)曲線相關(guān)
UIViewAnimationOptionCurveEaseInOut //時間曲線函數(shù),緩入緩出,中間快
UIViewAnimationOptionCurveEaseIn //時間曲線函數(shù),由慢到特別快(緩入快出)
UIViewAnimationOptionCurveEaseOut //時間曲線函數(shù),由快到慢(快入緩出)
UIViewAnimationOptionCurveLinear //時間曲線函數(shù),勻速
//轉(zhuǎn)場動畫相關(guān)的
UIViewAnimationOptionTransitionNone //無轉(zhuǎn)場動畫
UIViewAnimationOptionTransitionFlipFromLeft //轉(zhuǎn)場從左翻轉(zhuǎn)
UIViewAnimationOptionTransitionFlipFromRight //轉(zhuǎn)場從右翻轉(zhuǎn)
UIViewAnimationOptionTransitionCurlUp //上卷轉(zhuǎn)場
UIViewAnimationOptionTransitionCurlDown //下卷轉(zhuǎn)場
UIViewAnimationOptionTransitionCrossDissolve //轉(zhuǎn)場交叉消失
UIViewAnimationOptionTransitionFlipFromTop //轉(zhuǎn)場從上翻轉(zhuǎn)
UIViewAnimationOptionTransitionFlipFromBottom //轉(zhuǎn)場從下翻轉(zhuǎn)
所以,解決方案就出來了:
/**點擊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) {
}];
}
補充:
關(guān)于轉(zhuǎn)場動畫,它一般是用在下面這個方法中的:
[UIView transitionFromView: toView: duration: options: completion:^(BOOL finished) {}];
該方法效果是從一個view to 另一個view,期間可以使用一些轉(zhuǎn)場動畫效果。
總結(jié):
bug就像象牙塔,我們卡在那的時候,感覺無比的草蛋。但是解決后,回望一下,原來如此簡單...