iOS動畫——文字晃動

好比密碼錯誤提示,須要文字左右晃動幾回,以起到警示的做用。動畫

爲了調用使用方便,作一個分類。代碼以下:spa

 

.h文件3d

#import <QuartzCore/QuartzCore.h>

@interface CALayer (Anim)

/*
 *  搖動動畫
 */
-(void)shake;

@end

.m文件code

 

#import "CALayer+Anim.h"

@implementation CALayer (Anim)


/*
 *  搖動
 */
-(void)shake{
    
    CAKeyframeAnimation *kfa = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
    
    CGFloat s = 5;
    
    kfa.values = @[@(-s),@(0),@(s),@(0),@(-s),@(0),@(s),@(0)];
    
    //時長
    kfa.duration = 0.3f;
    
    //重複
    kfa.repeatCount = 2;
    
    //移除
    kfa.removedOnCompletion = YES;
    
    [self addAnimation:kfa forKey:@"shake"];
}

@end

 

 

 

 

使用方法,很是簡單,導入這個頭文件,以下調用:orm

   UILabel * wrongTip   = [[UILabel alloc]initWithFrame:CGRectMake(20, 130*ScreenHeightRate+heightBit+heightBit2, self.messageView.width-40, 15)];
    wrongTip.textColor     = [@"ff0000" hexColor];
    wrongTip.text          = @"券商密碼錯誤,請從新輸入";
    wrongTip.font          = [UIFont HeitiSCWithFontSize:12.f];
    wrongTip.textAlignment = NSTextAlignmentLeft;
    [self.messageView addSubview:wrongTip];
    
    // shake 動畫
    [wrongTip.layer shake];
blog

相關文章
相關標籤/搜索