iOS UIView Block 動畫- (基礎動畫, 關鍵幀動畫, 動畫組)

1霹琼、UIView本身對于基本動畫和關鍵幀動畫、轉場動畫都有相應的封裝凉当,在對動畫細節(jié)沒有特殊要求的情況下使用起來也要簡單的多

#import "ViewController.h"

@interface ViewController ()<CAAnimationDelegate>
@property(nonatomic, strong) UIImageView *firstImgView;
@property(nonatomic, strong) UIImageView * secondImgView;
@property(nonatomic, strong) UIImageView * receiveImgView;
@property(nonatomic, strong) UILabel * titleLab;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _firstImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 240*0.8, 295*0.8)];
    _firstImgView.center = self.view.center;
    _firstImgView.image = [UIImage imageNamed:@"2.png"];
    _firstImgView.alpha = 0.0;
    [self.view addSubview:_firstImgView];
    
    
    _secondImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 240*0.8, 295*0.8)];
    _secondImgView.center = self.view.center;
    _secondImgView.image = [UIImage imageNamed:@"1.png"];
    _secondImgView.hidden = YES;
    [self.view addSubview:_secondImgView];
    
    _titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 240*0.8, 30*0.8)];
    _titleLab.textAlignment = NSTextAlignmentCenter;
    _titleLab.text = @"恭喜獲得15張試聊卡!";
//    _titleLab.font = [UIFont systemFontOfSize:18];
     [_titleLab setAdjustsFontSizeToFitWidth:YES];
   _titleLab.adjustsFontSizeToFitWidth = YES;
   _titleLab.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    _titleLab.textColor = [UIColor whiteColor];
    [_secondImgView addSubview:_titleLab];

    
    CGFloat x = _secondImgView.center.x;
    CGFloat y = CGRectGetMaxY(_secondImgView.bounds) +100 +_secondImgView.bounds.size.height ;
    _receiveImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 180, 48)];
    _receiveImgView.center = CGPointMake(x, y);
    _receiveImgView.image = [UIImage imageNamed:@"3.png"];
    _receiveImgView.alpha = 0.0;
    [self.view addSubview:_receiveImgView];
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

    
    // 設置透視變換矩陣
    CATransform3D perspectiveTransform = CATransform3DIdentity;
    perspectiveTransform.m34 = -1.f/700;
    // 繞y軸旋轉π
    CATransform3D transform = CATransform3DRotate(perspectiveTransform, M_PI, 0, 1, 0);

    CABasicAnimation * animation_1 = [CABasicAnimation animation];
    animation_1.keyPath = @"transform";
    animation_1.duration = 0.083;
    animation_1.removedOnCompletion = NO;
    animation_1.fillMode = kCAFillModeForwards;
    animation_1.repeatCount = 10;
    animation_1.toValue = [NSValue valueWithCATransform3D:transform];

    [_firstImgView.layer addAnimation:animation_1 forKey:@"animation_1"];
    
    
   NSTimer *timer = [NSTimer timerWithTimeInterval:0.83 repeats:NO block:^(NSTimer * _Nonnull timer) {
       [_firstImgView.layer removeAnimationForKey:@"animation_1"];
       CABasicAnimation * animation_2 = [CABasicAnimation animation];
       animation_2.keyPath = @"transform";
       animation_2.duration = 0.21;
       animation_2.removedOnCompletion = NO;
       animation_2.fillMode = kCAFillModeForwards;
       animation_2.repeatCount = 2;
       animation_2.toValue = [NSValue valueWithCATransform3D:transform];
       [_firstImgView.layer addAnimation:animation_2 forKey:@"animation_2"];
   }];
   [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

    [UIView animateKeyframesWithDuration:1.25 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.2 animations:^{
            _firstImgView.alpha = 1.0f;
        }];

    } completion:^(BOOL finish){
        [_firstImgView.layer removeAllAnimations];
        [_firstImgView removeFromSuperview];
        [self animateSeconde];
    }];


}

- (void)animateSeconde {
    _secondImgView.hidden = NO;
    CABasicAnimation * animation = [CABasicAnimation animation];
    animation.keyPath = @"transform";
    animation.duration = 0.25;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.repeatCount = 4;
    

    // 設置透視變換矩陣
    CATransform3D perspectiveTransform = CATransform3DIdentity;
    perspectiveTransform.m34 = -1.f/700;
    // 將透視變換復合到旋轉變換中
    // 繞y軸旋轉π
    CATransform3D transform = CATransform3DRotate(perspectiveTransform, M_PI, 0, 1, 0);

    animation.toValue = [NSValue valueWithCATransform3D:transform];
    [_secondImgView.layer addAnimation:animation forKey:@"secondLayer"];
//    [lab.layer addAnimation:animation forKey:@"secondLayer"];

    
    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 repeats:NO block:^(NSTimer * _Nonnull timer) {
        [_secondImgView.layer removeAnimationForKey:@"secondLayer"];
   
        [UIView animateKeyframesWithDuration:1.0 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{

            [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.125 animations:^{
                _secondImgView.transform = CGAffineTransformMakeScale(1.5, 1.5);

            }];
            [UIView addKeyframeWithRelativeStartTime:0.875 relativeDuration:0.875 animations:^{
                _secondImgView.transform = CGAffineTransformMakeScale(1.25, 1.25);

                
               
            }];
        } completion:^(BOOL finish){
             _receiveImgView.alpha = 1.0;
        }];
    }];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];


}

@end

獲得卡牌.gif

2枣申、貝塞爾曲線

- (void)circleAnimationTypeOne
{
    
    //創(chuàng)建出CAShapeLayer
    self.shapeLayer = [CAShapeLayer layer];
    self.shapeLayer.frame = self.rect;
    self.shapeLayer.fillColor = [UIColor whiteColor].CGColor;
    
    //設置線條的寬度和顏色
    self.shapeLayer.lineWidth = self.lineWidth;
    self.shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor;
    
    
    //創(chuàng)建出圓形貝塞爾曲線
    
    UIBezierPath* circlePath=[UIBezierPath bezierPathWithArcCenter:CGPointMake(self.rect.size.width / 2, self.rect.size.height / 2 )radius:self.rect.size.height / 2 startAngle:M_PI_2 endAngle:2.5*M_PI  clockwise:YES];

    
    //讓貝塞爾曲線與CAShapeLayer產(chǎn)生聯(lián)系
    self.shapeLayer.path = circlePath.CGPath;
    
    //添加并顯示
    [self.layer addSublayer:self.shapeLayer];
    
    
    
    
    
    UIBezierPath* circlePath2=[UIBezierPath bezierPathWithArcCenter:CGPointMake(self.rect.size.width / 2, self.rect.size.height / 2 )radius:self.rect.size.height / 2 startAngle:M_PI_2 endAngle:2*M_PI*_haveFinished+M_PI_2 clockwise:YES];
    
    
    
    //創(chuàng)建出CAShapeLayer
    self.shapeLayer2 = [CAShapeLayer layer];
    self.shapeLayer2.frame = self.rect;
    
    self.shapeLayer2.fillColor = [UIColor clearColor].CGColor;
    
    //設置線條的寬度和顏色
    self.shapeLayer2.lineWidth = self.lineWidth;
    self.shapeLayer2.strokeColor =  kCColor(0,174,239).CGColor;

    
    //讓貝塞爾曲線與CAShapeLayer產(chǎn)生聯(lián)系
    self.shapeLayer2.path = circlePath2.CGPath;
    
    //添加并顯示
    [self.layer addSublayer:self.shapeLayer2];
    
    
    UIView* view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    view.center = CGPointMake(self.rect.size.width / 2, self.rect.size.height);
    view.layer.cornerRadius=10;
    view.layer.masksToBounds=YES;
    view.backgroundColor= kCColor(0,174,239);
    _roundView=view;
    [self addSubview:view];
    
}
flowQuery.gif

demo下載:

更多詳情請參考:
iOS動畫,絕對夠分量看杭!
iOS開發(fā)系列--讓你的應用“動”起來

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末忠藤,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子楼雹,更是在濱河造成了極大的恐慌模孩,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,123評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件烘豹,死亡現(xiàn)場離奇詭異瓜贾,居然都是意外死亡,警方通過查閱死者的電腦和手機携悯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評論 2 384
  • 文/潘曉璐 我一進店門祭芦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人憔鬼,你說我怎么就攤上這事龟劲。” “怎么了轴或?”我有些...
    開封第一講書人閱讀 156,723評論 0 345
  • 文/不壞的土叔 我叫張陵昌跌,是天一觀的道長。 經(jīng)常有香客問我照雁,道長蚕愤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,357評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮萍诱,結果婚禮上悬嗓,老公的妹妹穿的比我還像新娘。我一直安慰自己裕坊,他們只是感情好包竹,可當我...
    茶點故事閱讀 65,412評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著籍凝,像睡著了一般周瞎。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上饵蒂,一...
    開封第一講書人閱讀 49,760評論 1 289
  • 那天声诸,我揣著相機與錄音,去河邊找鬼苹享。 笑死双絮,一個胖子當著我的面吹牛,可吹牛的內容都是我干的得问。 我是一名探鬼主播囤攀,決...
    沈念sama閱讀 38,904評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼宫纬!你這毒婦竟也來了焚挠?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,672評論 0 266
  • 序言:老撾萬榮一對情侶失蹤漓骚,失蹤者是張志新(化名)和其女友劉穎蝌衔,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蝌蹂,經(jīng)...
    沈念sama閱讀 44,118評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡噩斟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,456評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了孤个。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片剃允。...
    茶點故事閱讀 38,599評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖齐鲤,靈堂內的尸體忽然破棺而出斥废,到底是詐尸還是另有隱情,我是刑警寧澤给郊,帶...
    沈念sama閱讀 34,264評論 4 328
  • 正文 年R本政府宣布牡肉,位于F島的核電站,受9級特大地震影響淆九,放射性物質發(fā)生泄漏统锤。R本人自食惡果不足惜毛俏,卻給世界環(huán)境...
    茶點故事閱讀 39,857評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望饲窿。 院中可真熱鬧拧抖,春花似錦、人聲如沸免绿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,731評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽嘲驾。三九已至,卻和暖如春迹卢,著一層夾襖步出監(jiān)牢的瞬間辽故,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,956評論 1 264
  • 我被黑心中介騙來泰國打工腐碱, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留誊垢,地道東北人。 一個月前我還...
    沈念sama閱讀 46,286評論 2 360
  • 正文 我出身青樓症见,卻偏偏與公主長得像喂走,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子谋作,可洞房花燭夜當晚...
    茶點故事閱讀 43,465評論 2 348

推薦閱讀更多精彩內容