左右抖動的動畫效果可以應用在登陸界面,當密碼或賬戶輸錯的時候,可以加入這個效果,也是不錯的,雖然low點,但起碼也是一種效果.嘿嘿...廢話不多說直接上代碼.
一、自己創建一個UIView的分類吧,這里我就不多說真么創建了
typedefvoid(^DyAnimationFinished)(void); ?//創建一個完成后的代碼塊
@interfaceUIView (SimpleAnimation)
-(void) shakeWithFinished:(DyAnimationFinished)finished;
@end
@implementationUIView (SimpleAnimation)
// 三個方法實現
/**左右搖擺shake*/
-(void) shakeWithFinished:(DyAnimationFinished)finished{
floatshakeMax_x ?= 10;//一次最大搖擺x坐標
floattime ?= 0.15;
__weaktypeof(self)weakSelf ?= self;
[selfsetMoveX:-shakeMax_xdrution:timefinished:^{ //先往左移動10
[weakSelfsetMoveX:shakeMax_x*2drution:timefinished:^{ //在往有移動20,相當在最原來的位置往右移動了10
[weakSelfsetMoveX:-shakeMax_x*2drution:timefinished:^{ //在往左移動20,相當于在上面的位置往左移動了10
[weakSelfsetMoveX:shakeMax_xdrution:timefinished:^{
if(finished) { ?
finished();
}
}];
}];
}];
}];
}
//重新設置x坐標
-(void) setMoveX:(CGFloat)x drution:(NSTimeInterval)time finished:(DyAnimationFinished)finished{
//調用重新設置此控件frame的方法,原來的x加上要移動的x
[selfsetX:(self.frame.origin.x+ x)drution:timefinished:finished];
}
//重新設置此控件frame的方法
-(void) setX:(CGFloat)total_x drution:(NSTimeInterval)time finished:(DyAnimationFinished)finished{
[UIViewanimateWithDuration:timeanimations:^{
CGRectframe ?= self.frame;
frame.origin.x = ?total_x;
self.frame = ?frame;
}completion:^(BOOLf) {
if(finished) {
finished();
}
}];
}
@end
二、這里我在補充一個小技巧,也是適用于登陸或是有輸入框的界面的技巧:監聽TextField的變化來改變按鈕的可點擊狀態.代碼如下:
1、[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(textDidChange)name:UITextFieldTextDidChangeNotificationobject:nil];
2、-(void) textDidChange{
if(_text_user.text.length == 0) {
_button.backgroundColor = ?[UIColor lightGrayColor];
_button.userInteractionEnabled=NO;
}else{
_button.backgroundColor= [UIColor orangeColor];
_button.userInteractionEnabled = YES;
}
}
//記得移除監聽
-(void) dealloc{
[[NSNotificationCenterdefaultCenter] removeObserver:self]; ?}
分享出來大家一起來學習!有什么更好的想法可以@我,一起進步!謝謝