- GitHub源碼:AFViewShaker
- star:1100+
??????
以下內(nèi)容來源于官方源碼、 README 文檔、測試 Demo或個人使用總結(jié) !
AFViewShaker
[圖片上傳失敗...(image-a1b1a5-1510132021807)]
[圖片上傳失敗...(image-914a1b-1510132021807)]
[圖片上傳失敗...(image-3b1e4c-1510132021807)]
About
AFViewShaker 實現(xiàn)了簡單的 UIView 磚塊抖動效果。

使用
為一個視圖創(chuàng)建抖動效果
AFViewShaker * viewShaker = [[AFViewShaker alloc] initWithView:self.formView];
為多個視圖創(chuàng)建抖動效果
NSArray * allFields = @[self.emailField, self.passwordField];
AFViewShaker * viewShaker = [[AFViewShaker alloc] initWithViewsArray:allFields];
使用默認參數(shù)抖動
// 抖動 AFViewShaker 實例中的所有視圖
[self.viewShaker shake];
// 抖動指定的視圖
[[[AFViewShaker alloc] initWithView:self.allButtons[0]] shake];
使用附帶參數(shù)配置抖動
[self.viewShaker shakeWithDuration:0.6 completion:^{
NSLog(@"Hello World!");
}];
源碼
AFViewShaker.h
// 2個初始化方法
- (instancetype)initWithView:(UIView *)view;
- (instancetype)initWithViewsArray:(NSArray *)viewsArray;
// 2個抖動方法
- (void)shake;
- (void)shakeWithDuration:(NSTimeInterval)duration completion:(void (^)())completion;
AFViewShaker.m
//
// AFViewShaker
// AFViewShaker
//
// Created by Philip Vasilchenko on 03.12.13.
// Copyright (c) 2014 okolodev. All rights reserved.
//
#import "AFViewShaker.h"
// 默認延時
static NSTimeInterval const kAFViewShakerDefaultDuration = 0.5;
static NSString * const kAFViewShakerAnimationKey = @"kAFViewShakerAnimationKey";
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 100000
// CAAnimationDelegate is not available before iOS 10 SDK
@interface AFViewShaker ()
#else
@interface AFViewShaker () <CAAnimationDelegate>
#endif
/** 包含視圖的數(shù)組 */
@property (nonatomic, strong) NSArray * views;
/** 完成動畫 */
@property (nonatomic, assign) NSUInteger completedAnimations;
/** 完成后調(diào)用 Block 對象 */
@property (nonatomic, copy) void (^completionBlock)();
@end
@implementation AFViewShaker
- (instancetype)initWithView:(UIView *)view {
// 這里只是調(diào)用了指定初始化方法,往數(shù)組里扔了一個 View
return [self initWithViewsArray:@[view]];
}
// 指定初始化方法
- (instancetype)initWithViewsArray:(NSArray *)viewsArray {
self = [super init];
if (self) {
self.views = viewsArray;
}
return self;
}
#pragma mark - Public methods
- (void)shake {
// 這里調(diào)用了下面的默認指定動畫方法,傳入的延時是全局靜態(tài)變量
[self shakeWithDuration:kAFViewShakerDefaultDuration completion:nil];
}
// 指定動畫方法
- (void)shakeWithDuration:(NSTimeInterval)duration completion:(void (^)())completion {
self.completionBlock = completion;
for (UIView * view in self.views) {
// 遍歷視圖,為每個視圖添加動畫
[self addShakeAnimationForView:view withDuration:duration];
}
}
#pragma mark - Shake Animation
- (void)addShakeAnimationForView:(UIView *)view withDuration:(NSTimeInterval)duration {
CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
CGFloat currentTx = view.transform.tx;
animation.delegate = self;
animation.duration = duration;
// 偏移量
animation.values = @[@(currentTx),
@(currentTx + 10),
@(currentTx - 8),
@(currentTx + 8),
@(currentTx - 5),
@(currentTx + 5),
@(currentTx)];
// 動畫時間
animation.keyTimes = @[@(0), @(0.225), @(0.425), @(0.6), @(0.75), @(0.875), @(1)];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[view.layer addAnimation:animation forKey:kAFViewShakerAnimationKey];
}
#pragma mark - CAAnimation Delegate
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag {
self.completedAnimations += 1;
if (self.completedAnimations >= self.views.count) {
self.completedAnimations = 0;
if (self.completionBlock) {
// 遍歷視圖,當(dāng)所有視圖的動畫完成以后就調(diào)用 Block 對象
self.completionBlock();
}
}
}
@end
- 配合 MBProgressHUD 使用:
//--------------------------------
// .h
/**
輸入文本框抖動效果
@param view 需要抖動的文本框視圖
@param text 抖動結(jié)束后顯示的 HUD 提示
@param HUDView 呈現(xiàn) HUD 的視圖
*/
- (void) shakeWithView:(UIView *)view
HUDText:(NSString *)text
addToView:(UIView *)HUDView;
//--------------------------------
// .m
- (void) shakeWithView:(UIView *)view
HUDText:(NSString *)text
addToView:(UIView *)HUDView {
AFViewShaker *viewShaker = [[AFViewShaker alloc] initWithView:view];
[viewShaker shakeWithDuration:KAFViewShakerDuration
completion:^{
[MBProgressHUD hql_showTextHUD:text toView:HUDView];
}];
}