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