實(shí)現(xiàn)視圖抖動(dòng)效果的思路
一精续、首先,如何讓視圖發(fā)生位移
UIView中定義了transform屬性箍铲,其類型為CGAffineTransform拨齐,默認(rèn)是CGAffineTransformIdentity
transform 改變位置/尺寸/旋轉(zhuǎn)角度,且是在原有transform的基礎(chǔ)上改變
①創(chuàng)建“基于控件初始位置”的形變
CGAffineTransformMakeTranslation(平移)
CGAffineTransformMakeScale(縮放)
CGAffineTransformMakeRotation(旋轉(zhuǎn))
②創(chuàng)建“基于transform參數(shù)”的形變
CGAffineTransformTranslate
CGAffineTransformScale
CGAffineTransformRotate
補(bǔ)充:
在OC中盛嘿,所有跟角度相關(guān)的數(shù)值洛巢,都是弧度值,180° = M_PI
正數(shù)表示順時(shí)針旋轉(zhuǎn)
負(fù)數(shù)表示逆時(shí)針旋轉(zhuǎn)
二次兆、如何讓視圖的位移形成反復(fù)動(dòng)畫(huà)
在UIViewAnimationWithBlocks中提供了一系列的視圖動(dòng)畫(huà)
我們最常用的也就是下面三種:
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL
其中UIViewAnimationOptions枚舉值如下:
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
UIViewAnimationOptionLayoutSubviews = 1 << 0, // 提交動(dòng)畫(huà)的時(shí)候布局子控件稿茉,表示子控件將和父控件一同動(dòng)畫(huà)
UIViewAnimationOptionAllowUserInteraction = 1 << 1, // 動(dòng)畫(huà)時(shí)允許用戶交互
UIViewAnimationOptionBeginFromCurrentState = 1 << 2, // 從當(dāng)前狀態(tài)開(kāi)始動(dòng)畫(huà),而不是初始狀態(tài)
UIViewAnimationOptionRepeat = 1 << 3, // 動(dòng)畫(huà)無(wú)限重復(fù)
UIViewAnimationOptionAutoreverse = 1 << 4, // 在動(dòng)畫(huà)可以重復(fù)的前提下芥炭,可執(zhí)行動(dòng)畫(huà)回路
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, // 忽略外層動(dòng)畫(huà)嵌套的執(zhí)行時(shí)間
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, // 忽略外層動(dòng)畫(huà)嵌套的時(shí)間變化曲線
UIViewAnimationOptionAllowAnimatedContent = 1 << 7, // animate contents (applies to transitions only)
UIViewAnimationOptionShowHideTransitionViews = 1 << 8, // 用顯隱的方式替代添加移除圖層的動(dòng)畫(huà)效果
UIViewAnimationOptionOverrideInheritedOptions = 1 << 9, // 忽略嵌套繼承的選項(xiàng)
///時(shí)間函數(shù)曲線相關(guān)
UIViewAnimationOptionCurveEaseInOut = 0 << 16, // 時(shí)間曲線函數(shù)漓库,由慢到快 default
UIViewAnimationOptionCurveEaseIn = 1 << 16, // 時(shí)間曲線函數(shù),由慢到特別快
UIViewAnimationOptionCurveEaseOut = 2 << 16, //時(shí)間曲線函數(shù)园蝠,由快到慢
UIViewAnimationOptionCurveLinear = 3 << 16, //時(shí)間曲線函數(shù)渺蒿,勻速
///轉(zhuǎn)場(chǎng)動(dòng)畫(huà)相關(guān)的
UIViewAnimationOptionTransitionNone = 0 << 20, //無(wú)轉(zhuǎn)場(chǎng)動(dòng)畫(huà) default
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20, //轉(zhuǎn)場(chǎng)從左翻轉(zhuǎn)
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20, //轉(zhuǎn)場(chǎng)從右翻轉(zhuǎn)
UIViewAnimationOptionTransitionCurlUp = 3 << 20, //上卷轉(zhuǎn)場(chǎng)
UIViewAnimationOptionTransitionCurlDown = 4 << 20, //下卷轉(zhuǎn)場(chǎng)
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20, //轉(zhuǎn)場(chǎng)交叉消失
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20, //轉(zhuǎn)場(chǎng)從上翻轉(zhuǎn)
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20, //轉(zhuǎn)場(chǎng)從下翻轉(zhuǎn)
UIViewAnimationOptionPreferredFramesPerSecondDefault = 0 << 24,
UIViewAnimationOptionPreferredFramesPerSecond60 = 3 << 24,
UIViewAnimationOptionPreferredFramesPerSecond30 = 7 << 24,
} NS_ENUM_AVAILABLE_IOS(4_0);
補(bǔ)充:關(guān)于最后一組轉(zhuǎn)場(chǎng)動(dòng)畫(huà)它一般是用在這個(gè)方法中的:
[UIView transitionFromView: toView: duration: options: completion:^(BOOL finished) {}];
三、基礎(chǔ)了解完畢彪薛,上代碼
+ (void)shakeView:(UIView*)viewToShake {
// 偏移值
CGFloat t = 3.0;
// 左搖
CGAffineTransform translateLeft =CGAffineTransformTranslate(CGAffineTransformIdentity,-t,0.0);
// 右晃
CGAffineTransform translateRight =CGAffineTransformTranslate(CGAffineTransformIdentity, t,0.0);
// 執(zhí)行動(dòng)畫(huà) 重復(fù)動(dòng)畫(huà)且執(zhí)行動(dòng)畫(huà)回路
viewToShake.transform = translateLeft;
[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
// 設(shè)置重復(fù)次數(shù) repeatCount為float
[UIView setAnimationRepeatCount:2.0];
viewToShake.transform = translateRight;
} completion:^(BOOL finished){
if(finished){
// 從視圖當(dāng)前狀態(tài)回到初始狀態(tài)
[UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
viewToShake.transform =CGAffineTransformIdentity;
} completion:NULL];
}
}];
}