Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> UI07UIView動畫

UI07UIView動畫

編輯:關於Android編程

ViewController.m

#import "ViewController.h"
#import "ImageAnimationViewController.h"
@interface ViewController ()
//黑色視圖
@property (nonatomic, strong) UIView *blackView;
//導航欄的按鈕點擊事件
- (IBAction)nextVC:(id)sender;
//初始化界面
- (void)initUserInterface;
#pragma mark - UIView動畫
////手勢方法
//- (void)tapProcess;
////位移
//- (void)positionAnimation;
////縮放
//- (void)scaleAnimation;
////旋轉
//- (void)rotationAnimation;
////變色
//- (void)colorAnimation;
////透明度
//- (void)alphAnimation;
////結束
//- (void)endAnimation;
////UIView動畫回調方法
//- (void)showViewAnimationDidStop:(NSString *)animationID;
#pragma mark - block方法
//位移
- (void)positionAnimationBlock;
//縮放
- (void)scaleAnimationBlock;
//旋轉
- (void)rotationAnimationBlock;
//顏色
- (void)colorAnimationBlock;
//透明度
- (void)alphAnimationBlock;
//返回
- (void)endAnimationBlock;

@end

@implementation ViewController

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

#pragma mark - initUserInterface method
- (void)initUserInterface
{
    //黑色背景視圖
    self.view.backgroundColor = [UIColor whiteColor];
    _blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    _blackView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_blackView];
    
    /**< 為黑色背景視圖添加點擊手勢 */
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapProcess)];
    [_blackView addGestureRecognizer:tapGesture];
}

#pragma mark - 手勢方法
- (void)tapProcess{
    [self positionAnimationBlock];
    
}

#pragma block動畫
//位移
- (void)positionAnimationBlock{
    [UIView animateWithDuration:0.5 animations:^{
        _blackView.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)- 100);
    } completion:^(BOOL finished) {
        [self scaleAnimationBlock];
    }];
}

//縮放
- (void)scaleAnimationBlock{
    [UIView animateWithDuration:0.5 animations:^{
        _blackView.transform = CGAffineTransformMakeScale(1.5, 1.5);
    } completion:^(BOOL finished) {
        [self rotationAnimationBlock];
    }];
}

//旋轉
- (void)rotationAnimationBlock{
    [UIView animateWithDuration:0.5 animations:^{
        _blackView.transform = CGAffineTransformRotate(_blackView.transform, M_PI);
    } completion:^(BOOL finished) {
        [self colorAnimationBlock];
    }];
}

//變色
- (void)colorAnimationBlock
{
    [UIView animateWithDuration:0.5 animations:^{
        _blackView.backgroundColor = [UIColor redColor];
    } completion:^(BOOL finished) {
        [self alphAnimationBlock];
    }];
}

//透明度(漸變)
- (void)alphAnimationBlock
{
    [UIView animateWithDuration:0.5 animations:^{
        _blackView.alpha = 0.5;
    } completion:^(BOOL finished) {
        [self endAnimationBlock];
    }];
}

//結束
- (void)endAnimationBlock{
    [UIView animateWithDuration:1 animations:^{
        //視圖恢復到初始狀態
        _blackView.backgroundColor = [UIColor blackColor];
        _blackView.alpha = 1.0;
        _blackView.center = CGPointMake(CGRectGetMidX(_blackView.bounds), CGRectGetMidX(_blackView.bounds));
        
        // CGAffineTransformIdentity:移除所有的變幻屬性
        _blackView.transform = CGAffineTransformIdentity;
        
        //轉場動畫(效果)
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_blackView cache:NO];
    } completion:^(BOOL finished) {

    }];
}

#pragma mark - UIView動畫
////位移
//- (void)positionAnimation{
//    //開始動畫
//    [UIView beginAnimations:@"position" context:nil];
//    //持續時間
//    [UIView setAnimationDuration:1];
//    //線性規律
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    //添加代理
//    [UIView setAnimationDelegate:self];
//    //設置回調方法
//    [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//    //核心動畫
//    _blackView.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)- 100);
//    //提交動畫
//    [UIView commitAnimations];
//}
//
////縮放
//- (void)scaleAnimation{
//    //開始動畫
//    [UIView beginAnimations:@"scale" context:nil];
//    //持續時間
//    [UIView setAnimationDuration:0.7];
//    //線性規律
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
//    //添加代理
//    [UIView setAnimationDelegate:self];
//    //設置回調方法
//    [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//    
//    //核心動畫
//    /**<  設置動畫類型:縮放
//      transform:變化屬性(可實現旋轉和所縮放動畫)
//      CGAffineTransformScale...:在新生成的屬性變換基礎之上繼續執行
//      CGAffineTransformMakeScale...:在最初始(最原始)的屬性基礎上做變化 */
//    
//    _blackView.transform = CGAffineTransformMakeScale(1.5, 1.5);
//    
//    //在變化後的基礎上還可持續放大
//    //_blackView.transform = CGAffineTransformScale(_blackView.transform, 1.5, 1.5);
//    //提交動畫
//    [UIView commitAnimations];
//}
//
////旋轉
//- (void)rotationAnimation{
//    //開始動畫
//    [UIView beginAnimations:@"rotation" context:nil];
//    //持續時間
//    [UIView setAnimationDuration:0.5];
//    //線性規律
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    //添加代理
//    [UIView setAnimationDelegate:self];
//    //設置回調方法
//    [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//    
//    // 設置動畫類型:旋轉
//    // angle:角度
//    // M_PI:180°
//    // M_PI_2:90°
//    // M_PI_4:45°
//    //旋轉之後縮回原來的大小
//    //_blackView.transform = CGAffineTransformMakeRotation(M_PI);
//    //旋轉之後保持放大後的大小
//    _blackView.transform = CGAffineTransformRotate(_blackView.transform, M_PI);
//    //提交動畫
//    [UIView commitAnimations];
//    
//}
//
////變色
//- (void)colorAnimation{
//    //開始動畫
//    [UIView beginAnimations:@"color" context:nil];
//    //持續時間
//    [UIView setAnimationDuration:0.5];
//    //線性規律
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    //添加代理
//    [UIView setAnimationDelegate:self];
//    //設置回調方法
//    [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//    //核心動畫
//    _blackView.backgroundColor = [UIColor redColor];
//     //提交動畫
//     [UIView commitAnimations];
//}
//
////透明度
//- (void)alphAnimation{
//    //開始動畫
//    [UIView beginAnimations:@"alph" context:nil];
//    //持續時間
//    [UIView setAnimationDuration:0.5];
//    //線性規律
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    //添加代理
//    [UIView setAnimationDelegate:self];
//    //設置回調方法
//    [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//    //核心動畫
//     _blackView.alpha = 0.5;
//    //提交動畫
//    [UIView commitAnimations];
//    
//}
//
////結束
//- (void)endAnimation{
//    //開始動畫
//    [UIView beginAnimations:@"end" context:nil];
//    //持續時間
//    [UIView setAnimationDuration:1];
//    //線性規律
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    //添加代理
//    [UIView setAnimationDelegate:self];
//    //設置回調方法
//    [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//    
//    //視圖恢復到初始狀態
//    _blackView.backgroundColor = [UIColor blackColor];
//    _blackView.alpha = 1.0;
//    _blackView.center = CGPointMake(CGRectGetMidX(_blackView.bounds), CGRectGetMidX(_blackView.bounds));
//    
//    // CGAffineTransformIdentity:移除所有的變幻屬性
//    _blackView.transform = CGAffineTransformIdentity;
//    
//    
//    //轉場動畫(效果)
//    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_blackView cache:NO];
//    
//    //提交動畫
//    [UIView commitAnimations];
//}
//
//#pragma mark - UIView動畫回調方法
//- (void)showViewAnimationDidStop:(NSString *)animationID{
//    if ([animationID isEqualToString:@"position"]) {
//        [self scaleAnimation];
//    }else if ([animationID isEqualToString:@"scale"]){
//        [self rotationAnimation];
//    }else if ([animationID isEqualToString:@"rotation"]){
//        [self colorAnimation];
//    }else if ([animationID isEqualToString:@"color"]){
//        [self alphAnimation];
//    }else if ([animationID isEqualToString:@"alph"]){
//        [self endAnimation];
//    }
//    
//}

- (IBAction)nextVC:(id)sender {
    ImageAnimationViewController *imageVC = [[ImageAnimationViewController alloc]init];
    [self.navigationController pushViewController:imageVC animated:YES];
}
@end

ImageAnimationViewController.m

 

 

#import "ImageAnimationViewController.h"
#define IMAGE_WITH_NAME(name)[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]]
#define SCREEN_WEIGH [UIScreen mainScreen].bounds.size.width

@interface ImageAnimationViewController ()

@property (nonatomic, strong) UIImageView *imageView;

- (void)initUserInterface;

@end

@implementation ImageAnimationViewController

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

- (void)initUserInterface{
    self.view.backgroundColor = [UIColor cyanColor];
    //
    
    NSMutableArray *imageArray = [NSMutableArray array];
    //創建圖片數組
    for (int i = 0; i < 138; i ++) {
        NSString *imageName = [NSString stringWithFormat:@"%d",i];
        //第一種
       // UIImage *image = [UIImage imageNamed:imageName];
        //第二種(加載大量的圖片)
//        NSString *pathString = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
//        UIImage *image = [UIImage imageWithContentsOfFile:pathString];
        //第三種(宏定義)
        UIImage *image = IMAGE_WITH_NAME(imageName);
         [imageArray addObject:image];
    }
    //創建圖片視圖
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, SCREEN_WEIGH, 400)];
    [self.view addSubview:imageView];
    
    //為其添加圖片數組
    imageView.animationImages = imageArray;
    //動畫時長
    imageView.animationDuration = 2;
    self.imageView = imageView;
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 100, 80, 60);
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:@"stop" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(stopProcess) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
}

- (void)stopProcess{
    [self.imageView stopAnimating];
}

//獲取點擊屏幕動作
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    // 1. 獲取touch
    UITouch *touch = [touches anyObject];
    // 2. 獲取當期點
    CGPoint point = [touch locationInView:self.view];
    NSLog(@"%@", NSStringFromCGPoint(point));
     // 3. 動畫開始
      [_imageView startAnimating];
}


- (void)viewWillDisappear:(BOOL)animated{
    //animationImages數組無法釋放,所以需要手動釋放,但是不能直接寫 self.catImageView.animationImages = nil;因為動畫是異步,還沒執行完,數組就釋放會崩潰的。所以可以使用performSelector讓動畫在剛好做完後延遲釋放,或者在viewWillDisappear
    
    self.imageView.animationImages = nil;
}

@end
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved