- GitHub源碼:AFViewShaker
- star:1100+
??????
以下內(nèi)容來源于官方源碼颖低、 README 文檔歧蕉、測(cè)試 Demo或個(gè)人使用總結(jié) 夫嗓!
AFViewShaker
[圖片上傳失敗...(image-a1b1a5-1510132021807)]
[圖片上傳失敗...(image-914a1b-1510132021807)]
[圖片上傳失敗...(image-3b1e4c-1510132021807)]
About
AFViewShaker 實(shí)現(xiàn)了簡(jiǎn)單的 UIView 磚塊抖動(dòng)效果裹唆。
使用
為一個(gè)視圖創(chuàng)建抖動(dòng)效果
AFViewShaker * viewShaker = [[AFViewShaker alloc] initWithView:self.formView];
為多個(gè)視圖創(chuàng)建抖動(dòng)效果
NSArray * allFields = @[self.emailField, self.passwordField];
AFViewShaker * viewShaker = [[AFViewShaker alloc] initWithViewsArray:allFields];
使用默認(rèn)參數(shù)抖動(dòng)
// 抖動(dòng) AFViewShaker 實(shí)例中的所有視圖
[self.viewShaker shake];
// 抖動(dòng)指定的視圖
[[[AFViewShaker alloc] initWithView:self.allButtons[0]] shake];
使用附帶參數(shù)配置抖動(dòng)
[self.viewShaker shakeWithDuration:0.6 completion:^{
NSLog(@"Hello World!");
}];
源碼
AFViewShaker.h
// 2個(gè)初始化方法
- (instancetype)initWithView:(UIView *)view;
- (instancetype)initWithViewsArray:(NSArray *)viewsArray;
// 2個(gè)抖動(dòng)方法
- (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"
// 默認(rèn)延時(shí)
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;
/** 完成動(dòng)畫 */
@property (nonatomic, assign) NSUInteger completedAnimations;
/** 完成后調(diào)用 Block 對(duì)象 */
@property (nonatomic, copy) void (^completionBlock)();
@end
@implementation AFViewShaker
- (instancetype)initWithView:(UIView *)view {
// 這里只是調(diào)用了指定初始化方法究珊,往數(shù)組里扔了一個(gè) 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)用了下面的默認(rèn)指定動(dòng)畫方法瓤摧,傳入的延時(shí)是全局靜態(tài)變量
[self shakeWithDuration:kAFViewShakerDefaultDuration completion:nil];
}
// 指定動(dòng)畫方法
- (void)shakeWithDuration:(NSTimeInterval)duration completion:(void (^)())completion {
self.completionBlock = completion;
for (UIView * view in self.views) {
// 遍歷視圖竿裂,為每個(gè)視圖添加動(dòng)畫
[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)];
// 動(dòng)畫時(shí)間
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)所有視圖的動(dòng)畫完成以后就調(diào)用 Block 對(duì)象
self.completionBlock();
}
}
}
@end
- 配合 MBProgressHUD 使用:
//--------------------------------
// .h
/**
輸入文本框抖動(dòng)效果
@param view 需要抖動(dòng)的文本框視圖
@param text 抖動(dòng)結(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];
}];
}