最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解iOS開發(fā)中的轉(zhuǎn)場動(dòng)畫和組動(dòng)畫以及UIView封裝動(dòng)畫

 更新時(shí)間:2015年11月19日 09:56:37   作者:文頂頂  
這篇文章主要介紹了iOS開發(fā)中的轉(zhuǎn)場動(dòng)畫和組動(dòng)畫以及UIView封裝動(dòng)畫,主要用到了CAAnimation類和UIView類,需要的朋友可以參考下

一、轉(zhuǎn)場動(dòng)畫

CAAnimation的子類,用于做轉(zhuǎn)場動(dòng)畫,能夠?yàn)閷犹峁┮瞥銎聊缓鸵迫肫聊坏膭?dòng)畫效果。iOS比Mac OS X的轉(zhuǎn)場動(dòng)畫效果少一點(diǎn)

UINavigationController就是通過CATransition實(shí)現(xiàn)了將控制器的視圖推入屏幕的動(dòng)畫效果

屬性解析:

type:動(dòng)畫過渡類型

subtype:動(dòng)畫過渡方向

startProgress:動(dòng)畫起點(diǎn)(在整體動(dòng)畫的百分比)

endProgress:動(dòng)畫終點(diǎn)(在整體動(dòng)畫的百分比)

轉(zhuǎn)場動(dòng)畫代碼示例

1.界面搭建

2015111995221439.png (331×491)

2.實(shí)現(xiàn)代碼

復(fù)制代碼 代碼如下:

//
//  YYViewController.m
//  13-轉(zhuǎn)場動(dòng)畫
//
//  Created by apple on 14-6-21.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()
@property(nonatomic,assign) int index;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;

- (IBAction)preOnClick:(UIButton *)sender;
- (IBAction)nextOnClick:(UIButton *)sender;

@end


復(fù)制代碼 代碼如下:

@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.index=1;

}

- (IBAction)preOnClick:(UIButton *)sender {
    self.index--;
    if (self.index<1) {
        self.index=7;
    }
    self.iconView.image=[UIImage imageNamed: [NSString stringWithFormat:@"%d.jpg",self.index]];
   
    //創(chuàng)建核心動(dòng)畫
    CATransition *ca=[CATransition animation];
    //告訴要;執(zhí)行什么動(dòng)畫
    //設(shè)置過度效果
    ca.type=@"cube";
    //設(shè)置動(dòng)畫的過度方向(向左)
    ca.subtype=kCATransitionFromLeft
    //設(shè)置動(dòng)畫的時(shí)間
    ca.duration=2.0;
    //添加動(dòng)畫
    [self.iconView.layer addAnimation:ca forKey:nil];
}

//下一張
- (IBAction)nextOnClick:(UIButton *)sender {
    self.index++;
    if (self.index>7) {
        self.index=1;
    }
        self.iconView.image=[UIImage imageNamed: [NSString stringWithFormat:@"%d.jpg",self.index]];
   
    //1.創(chuàng)建核心動(dòng)畫
    CATransition *ca=[CATransition animation];
   
    //1.1告訴要執(zhí)行什么動(dòng)畫
    //1.2設(shè)置過度效果
    ca.type=@"cube";
    //1.3設(shè)置動(dòng)畫的過度方向(向右)
    ca.subtype=kCATransitionFromRight;
    //1.4設(shè)置動(dòng)畫的時(shí)間
    ca.duration=2.0;
    //1.5設(shè)置動(dòng)畫的起點(diǎn)
    ca.startProgress=0.5;
    //1.6設(shè)置動(dòng)畫的終點(diǎn)
//    ca.endProgress=0.5;
   
    //2.添加動(dòng)畫
    [self.iconView.layer addAnimation:ca forKey:nil];
}
@end


點(diǎn)擊上一張,或者下一張的時(shí)候,展示對應(yīng)的動(dòng)畫效果。

2015111995322098.png (348×532)

二、組動(dòng)畫

CAAnimation的子類,可以保存一組動(dòng)畫對象,將CAAnimationGroup對象加入層后,組中所有動(dòng)畫對象可以同時(shí)并發(fā)運(yùn)行

屬性解析:

animations:用來保存一組動(dòng)畫對象的NSArray

默認(rèn)情況下,一組動(dòng)畫對象是同時(shí)運(yùn)行的,也可以通過設(shè)置動(dòng)畫對象的beginTime屬性來更改動(dòng)畫的開始時(shí)間

分組動(dòng)畫代碼示例

代碼:

復(fù)制代碼 代碼如下:

#import "YYViewController.h"

@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIView *iconView;

@end

@implementation NJViewController

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
    // 平移動(dòng)畫
    CABasicAnimation *a1 = [CABasicAnimation animation];
    a1.keyPath = @"transform.translation.y";
    a1.toValue = @(100);
    // 縮放動(dòng)畫
    CABasicAnimation *a2 = [CABasicAnimation animation];
    a2.keyPath = @"transform.scale";
    a2.toValue = @(0.0);
    // 旋轉(zhuǎn)動(dòng)畫
    CABasicAnimation *a3 = [CABasicAnimation animation];
    a3.keyPath = @"transform.rotation";
    a3.toValue = @(M_PI_2);
   
    // 組動(dòng)畫
    CAAnimationGroup *groupAnima = [CAAnimationGroup animation];
   
    groupAnima.animations = @[a1, a2, a3];
   
    //設(shè)置組動(dòng)畫的時(shí)間
    groupAnima.duration = 2;
    groupAnima.fillMode = kCAFillModeForwards;
    groupAnima.removedOnCompletion = NO;
   
    [self.iconView.layer addAnimation:groupAnima forKey:nil];
}

@end


說明:平移-旋轉(zhuǎn)-縮放作為一組動(dòng)畫一起執(zhí)行。

執(zhí)行效果:

2015111995345002.png (640×960)

三、UIView封裝動(dòng)畫
1.UIView動(dòng)畫(首尾)

(1).簡單說明

UIKit直接將動(dòng)畫集成到UIView類中,當(dāng)內(nèi)部的一些屬性發(fā)生改變時(shí),UIView將為這些改變提供動(dòng)畫支持

執(zhí)行動(dòng)畫所需要的工作由UIView類自動(dòng)完成,但仍要在希望執(zhí)行動(dòng)畫時(shí)通知視圖,為此需要將改變屬性的代碼放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之間

常見方法解析:

+ (void)setAnimationDelegate:(id)delegate     設(shè)置動(dòng)畫代理對象,當(dāng)動(dòng)畫開始或者結(jié)束時(shí)會(huì)發(fā)消息給代理對象

+ (void)setAnimationWillStartSelector:(SEL)selector   當(dāng)動(dòng)畫即將開始時(shí),執(zhí)行delegate對象的selector,并且把beginAnimations:context:中傳入的參數(shù)傳進(jìn)selector

+ (void)setAnimationDidStopSelector:(SEL)selector  當(dāng)動(dòng)畫結(jié)束時(shí),執(zhí)行delegate對象的selector,并且把beginAnimations:context:中傳入的參數(shù)傳進(jìn)selector

+ (void)setAnimationDuration:(NSTimeInterval)duration   動(dòng)畫的持續(xù)時(shí)間,秒為單位

+ (void)setAnimationDelay:(NSTimeInterval)delay  動(dòng)畫延遲delay秒后再開始

+ (void)setAnimationStartDate:(NSDate *)startDate   動(dòng)畫的開始時(shí)間,默認(rèn)為now

+ (void)setAnimationCurve:(UIViewAnimationCurve)curve  動(dòng)畫的節(jié)奏控制

+ (void)setAnimationRepeatCount:(float)repeatCount  動(dòng)畫的重復(fù)次數(shù)

+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses  如果設(shè)置為YES,代表動(dòng)畫每次重復(fù)執(zhí)行的效果會(huì)跟上一次相反

+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache  設(shè)置視圖view的過渡效果, transition指定過渡類型, cache設(shè)置YES代表使用視圖緩存,性能較好

(2).代碼示例

復(fù)制代碼 代碼如下:

//
//  YYViewController.m
//  01-uiview封裝動(dòng)畫
//
//  Created by apple on 14-6-22.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIView *customView;


@end


復(fù)制代碼 代碼如下:

@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
   
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //打印動(dòng)畫塊的位置
    NSLog(@"動(dòng)畫執(zhí)行之前的位置:%@",NSStringFromCGPoint(self.customView.center));
   
    //首尾式動(dòng)畫
    [UIView beginAnimations:nil context:nil];
    //執(zhí)行動(dòng)畫
    //設(shè)置動(dòng)畫執(zhí)行時(shí)間
    [UIView setAnimationDuration:2.0];
    //設(shè)置代理
    [UIView setAnimationDelegate:self];
    //設(shè)置動(dòng)畫執(zhí)行完畢調(diào)用的事件
    [UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
    self.customView.center=CGPointMake(200, 300);
    [UIView commitAnimations];

}

-(void)didStopAnimation
{
    NSLog(@"動(dòng)畫執(zhí)行完畢");
    //打印動(dòng)畫塊的位置
    NSLog(@"動(dòng)畫執(zhí)行之后的位置:%@",NSStringFromCGPoint(self.customView.center));
}
@end


執(zhí)行結(jié)果:

2015111995419071.png (320×502)2015111995436301.png (321×502)

打印動(dòng)畫塊的位置:

2015111995500715.png (881×101)

(3).UIView封裝的動(dòng)畫與CALayer動(dòng)畫的對比

使用UIView和CALayer都能實(shí)現(xiàn)動(dòng)畫效果,但是在真實(shí)的開發(fā)中,一般還是主要使用UIView封裝的動(dòng)畫,而很少使用CALayer的動(dòng)畫。

CALayer核心動(dòng)畫與UIView動(dòng)畫的區(qū)別:
UIView封裝的動(dòng)畫執(zhí)行完畢之后不會(huì)反彈。即如果是通過CALayer核心動(dòng)畫改變layer的位置狀態(tài),表面上看雖然已經(jīng)改變了,但是實(shí)際上它的位置是沒有改變的。

代碼示例:

復(fù)制代碼 代碼如下:

//
//  YYViewController.m
//  01-uiview封裝動(dòng)畫
//
//  Created by apple on 14-6-22.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIView *customView;


@end


復(fù)制代碼 代碼如下:

@implementation YYViewController

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   //1.創(chuàng)建核心動(dòng)畫
    CABasicAnimation *anima=[CABasicAnimation animation];
    //平移
    anima.keyPath=@"position";
    //設(shè)置執(zhí)行的動(dòng)畫
    anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)];
   
    //設(shè)置執(zhí)行動(dòng)畫的時(shí)間
    anima.duration=2.0;
    //設(shè)置動(dòng)畫執(zhí)行完畢之后不刪除動(dòng)畫
    anima.removedOnCompletion=NO;
    //設(shè)置保存動(dòng)畫的最新狀態(tài)
    anima.fillMode=kCAFillModeForwards;
//    anima.fillMode=kCAFillModeBackwards;
   
    //設(shè)置動(dòng)畫的代理
    anima.delegate=self;
   
    //2.添加核心動(dòng)畫
    [self.customView.layer addAnimation:anima forKey:nil];
}

-(void)animationDidStart:(CAAnimation *)anim
{
    //打印動(dòng)畫塊的位置
//    NSLog(@"動(dòng)畫開始執(zhí)行前的位置:%@",NSStringFromCGPoint(self.customView.center));
    NSLog(@"動(dòng)畫開始執(zhí)行前的位置:%@",NSStringFromCGPoint( self.customView.layer.position));
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    //打印動(dòng)畫塊的位置
    NSLog(@"動(dòng)畫執(zhí)行完畢后的位置:%@",NSStringFromCGPoint( self.customView.layer.position));
}

@end


打印結(jié)果:

2015111995529298.png (881×97)

2、block動(dòng)畫

(1).簡單說明

復(fù)制代碼 代碼如下:

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

參數(shù)解析:

duration:動(dòng)畫的持續(xù)時(shí)間

delay:動(dòng)畫延遲delay秒后開始

options:動(dòng)畫的節(jié)奏控制

animations:將改變視圖屬性的代碼放在這個(gè)block中

completion:動(dòng)畫結(jié)束后,會(huì)自動(dòng)調(diào)用這個(gè)block

轉(zhuǎn)場動(dòng)畫

復(fù)制代碼 代碼如下:

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

參數(shù)解析:

duration:動(dòng)畫的持續(xù)時(shí)間

view:需要進(jìn)行轉(zhuǎn)場動(dòng)畫的視圖

options:轉(zhuǎn)場動(dòng)畫的類型

animations:將改變視圖屬性的代碼放在這個(gè)block中

completion:動(dòng)畫結(jié)束后,會(huì)自動(dòng)調(diào)用這個(gè)block
 

復(fù)制代碼 代碼如下:

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion

方法調(diào)用完畢后,相當(dāng)于執(zhí)行了下面兩句代碼:
復(fù)制代碼 代碼如下:

// 添加toView到父視圖

[fromView.superview addSubview:toView];

// 把fromView從父視圖中移除

[fromView.superview removeFromSuperview];


參數(shù)解析:

duration:動(dòng)畫的持續(xù)時(shí)間

options:轉(zhuǎn)場動(dòng)畫的類型

animations:將改變視圖屬性的代碼放在這個(gè)block中

completion:動(dòng)畫結(jié)束后,會(huì)自動(dòng)調(diào)用這個(gè)block

 

(2).代碼示例

復(fù)制代碼 代碼如下:

#import "YYViewController.h"

@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIView *customView;

@end


復(fù)制代碼 代碼如下:

@implementation YYViewController

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //block代碼塊動(dòng)畫
        [UIView transitionWithView:self.customView duration:3.0 options:0 animations:^{
            //執(zhí)行的動(dòng)畫
            NSLog(@"動(dòng)畫開始執(zhí)行前的位置:%@",NSStringFromCGPoint(self.customView.center));
            self.customView.center=CGPointMake(200, 300);
        } completion:^(BOOL finished) {
            //動(dòng)畫執(zhí)行完畢后的首位操作
            NSLog(@"動(dòng)畫執(zhí)行完畢");
            NSLog(@"動(dòng)畫執(zhí)行完畢后的位置:%@",NSStringFromCGPoint( self.customView.center));
        }];
}
@end


打印結(jié)果:

2015111995606155.png (878×90)

提示:self.customView.layer.position和self.customView.center等價(jià),因?yàn)閜osition的默認(rèn)值為(0.5,0.5)。

3、補(bǔ)充

(1).UIImageView的幀動(dòng)畫

UIImageView可以讓一系列的圖片在特定的時(shí)間內(nèi)按順序顯示

相關(guān)屬性解析:

animationImages:要顯示的圖片(一個(gè)裝著UIImage的NSArray)

animationDuration:完整地顯示一次animationImages中的所有圖片所需的時(shí)間

animationRepeatCount:動(dòng)畫的執(zhí)行次數(shù)(默認(rèn)為0,代表無限循環(huán))

相關(guān)方法解析:

- (void)startAnimating; 開始動(dòng)畫

- (void)stopAnimating;  停止動(dòng)畫

- (BOOL)isAnimating;  是否正在運(yùn)行動(dòng)畫

(2).UIActivityIndicatorView

是一個(gè)旋轉(zhuǎn)進(jìn)度輪,可以用來告知用戶有一個(gè)操作正在進(jìn)行中,一般用initWithActivityIndicatorStyle初始化

方法解析:

- (void)startAnimating; 開始動(dòng)畫

- (void)stopAnimating;  停止動(dòng)畫

- (BOOL)isAnimating;  是否正在運(yùn)行動(dòng)畫

UIActivityIndicatorViewStyle有3個(gè)值可供選擇:

復(fù)制代碼 代碼如下:

UIActivityIndicatorViewStyleWhiteLarge   //大型白色指示器   

UIActivityIndicatorViewStyleWhite      //標(biāo)準(zhǔn)尺寸白色指示器   

UIActivityIndicatorViewStyleGray    //灰色指示器,用于白色背景


相關(guān)文章

  • Flutter繪制3.4邊形及多邊形漸變動(dòng)畫實(shí)現(xiàn)示例

    Flutter繪制3.4邊形及多邊形漸變動(dòng)畫實(shí)現(xiàn)示例

    這篇文章主要為大家介紹了Flutter繪制3.4邊形之多邊形漸變動(dòng)畫實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 12個(gè)iOS技術(shù)面試題及答案總結(jié)

    12個(gè)iOS技術(shù)面試題及答案總結(jié)

    這篇文章給大家總結(jié)了在iOS面試的時(shí)候可能會(huì)遇到的12個(gè)技術(shù)面試題,以及這些面試題但答案,這些答案只是給大家一些參考,大家可以再結(jié)合自己理解進(jìn)行回答,有需要的朋友們下面來一起看看吧。
    2016-09-09
  • 淺談iOS解析HTMl標(biāo)簽以及開發(fā)中的一些坑

    淺談iOS解析HTMl標(biāo)簽以及開發(fā)中的一些坑

    這篇文章主要介紹了淺談iOS解析HTMl標(biāo)簽以及開發(fā)中的一些坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • iOS中視頻播放器的簡單封裝詳解

    iOS中視頻播放器的簡單封裝詳解

    要實(shí)現(xiàn)封裝視頻播放器,首先需要實(shí)現(xiàn)視頻播放器,然后再去考慮怎樣封裝可以讓以后自己使用起來方便快捷。iOS9之前可以使用MediaPlayer來進(jìn)行視頻的播放,iOS9之后系統(tǒng)推薦使用AVFoundation框架實(shí)現(xiàn)視頻的播放。下面通過本文來看看詳細(xì)的介紹吧。
    2016-10-10
  • iOS中讀寫鎖的簡單實(shí)現(xiàn)方法實(shí)例

    iOS中讀寫鎖的簡單實(shí)現(xiàn)方法實(shí)例

    讀寫鎖是計(jì)算機(jī)程序的并發(fā)控制的一種同步機(jī)制,也稱“共享-互斥鎖”、多讀者-單寫者鎖,讀操作可并發(fā)重入,寫操作是互斥的,這篇文章主要給大家介紹了關(guān)于iOS中讀寫鎖的簡單實(shí)現(xiàn)方法,需要的朋友可以參考下
    2021-11-11
  • ios App加載本地HTML網(wǎng)頁,點(diǎn)擊網(wǎng)頁鏈接跳轉(zhuǎn)到app頁面的方法

    ios App加載本地HTML網(wǎng)頁,點(diǎn)擊網(wǎng)頁鏈接跳轉(zhuǎn)到app頁面的方法

    下面小編就為大家分享一篇ios App加載本地HTML網(wǎng)頁,點(diǎn)擊網(wǎng)頁鏈接跳轉(zhuǎn)到app頁面的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • iOS開發(fā)技巧之狀態(tài)欄字體顏色的設(shè)置方法

    iOS開發(fā)技巧之狀態(tài)欄字體顏色的設(shè)置方法

    有時(shí)候我們需要根據(jù)不同的背景修改狀態(tài)欄字體的顏色,下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)技巧之狀態(tài)欄字體顏色的設(shè)置方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧
    2018-08-08
  • iOS之加載Gif圖片的方法

    iOS之加載Gif圖片的方法

    本篇文章主要介紹了iOS之加載Gif圖片,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • iOS中表單列表樣式鍵盤遮擋的解決方案

    iOS中表單列表樣式鍵盤遮擋的解決方案

    這篇文章主要給大家介紹了關(guān)于iOS中表單列表樣式鍵盤遮擋的解決方案,文中通過示例代碼將解決的方法一步步介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧
    2019-01-01
  • iOS開發(fā)中使用FMDB來使程序連接SQLite數(shù)據(jù)庫

    iOS開發(fā)中使用FMDB來使程序連接SQLite數(shù)據(jù)庫

    這篇文章主要介紹了iOS開發(fā)中使用FMDB來使程序連接SQLite數(shù)據(jù)庫,SQLite是一個(gè)簡單的嵌入式數(shù)據(jù)庫,非常適合輕量級(jí)使用,需要的朋友可以參考下
    2015-11-11

最新評(píng)論

星子县| 双流县| 万年县| 平远县| 资溪县| 潼关县| 安西县| 曲靖市| 浏阳市| 宜州市| 伊宁市| 南皮县| 康保县| 循化| 高唐县| 南涧| 永年县| 京山县| 德庆县| 新密市| 平遥县| 叙永县| 西藏| 犍为县| 永丰县| 温泉县| 项城市| 彭州市| 福海县| 泾阳县| 保定市| 清水河县| 大同市| 江都市| 任丘市| 广汉市| 万源市| 米泉市| 汾西县| 荃湾区| 偃师市|