2012年10月23日 星期二

UIView的Animation(三)


Apple 提供了四種速度變化。


1. 開始時慢慢加速,結束前慢慢減速:

UIViewAnimationCurve curve = UIViewAnimationCurveEaseInOut;


2. 開始時慢慢加速:

UIViewAnimationCurve curve = UIViewAnimationCurveEaseIn;


3. 結束前慢慢減速:

UIViewAnimationCurve curve = UIViewAnimationCurveEaseOut;


4. 開始到尾也是均速:

UIViewAnimationCurve curve = UIViewAnimationCurveLinear;



以下是一個實作例
1. 開啓一個專案

2. 在storyBoard加上一個Button

3. 在mainViewController.h加上Button的註冊及一個Int變數
#import <UIKit/UIKit.h>

@interface mainViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *changeButton;

@property (nonatomic) int sel;


@end

4. 在mainViewController.m加上程式碼
#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController

@synthesize sel;
@synthesize changeButton;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    sel = 0;
   
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)changeModel:(id)sender {
   
    sel++;
   
    if (sel>4)
        sel = 1;
   
    if (sel == 1)
        [changeButton setTitle:@"UIViewAnimationCurveEaseInOut" forState:UIControlStateNormal];
    else if (sel == 2)
        [changeButton setTitle:@"UIViewAnimationCurveLinear" forState:UIControlStateNormal];
    else if (sel == 3)
        [changeButton setTitle:@"UIViewAnimationCurveEaseIn" forState:UIControlStateNormal];
    else
        [changeButton setTitle:@"UIViewAnimationCurveEaseOut" forState:UIControlStateNormal];
   
    [self moveLabel:sel];
   
}

- (void) moveLabel:(int)sel
{
    UILabel *_actionLabel = [UILabel new]; //產生一個動態的Label
   
    _actionLabel.frame = CGRectMake(0, 0, 100, 100);  //大小
   
    _actionLabel.center = self.view.center;  //位置在view的正中間
   
    UIColor *color;
   
    if (sel == 1)
        color = [UIColor blueColor];
    else if (sel == 2)
        color = [UIColor redColor];
    else if (sel == 3)
        color = [UIColor greenColor];
    else
        color = [UIColor grayColor];
   
    _actionLabel.backgroundColor = color; //底色為藍色
   
    _actionLabel.textAlignment = UITextAlignmentCenter; // 文字放置方法為對齊
   
    _actionLabel.text = @"Hello Action";//動態Label文字展示為Hello Action
   
    [self.view addSubview:_actionLabel];  //將動態的Label放到View上展示
   
    //UIViewAnimationTransition transition = UIViewAnimationTransitionCurlUp;
    //UIViewAnimationTransition transition = UIViewAnimationTransitionFlipFromLeft;
   
    UIViewAnimationCurve curve ;
   
    if (sel == 1)
        curve = UIViewAnimationCurveEaseInOut;
    else if (sel == 2)
        curve = UIViewAnimationCurveLinear;
    else if (sel == 3)
        curve = UIViewAnimationCurveEaseIn;
    else
        curve = UIViewAnimationCurveEaseOut;
   
    [UIView beginAnimations:@"anim" context:NULL];
    [UIView setAnimationCurve:curve];
    //[UIView setAnimationTransition:transition forView:[self view] cache:YES];
    [UIView setAnimationDuration:5];
   
    //[self.view setAlpha:0.3];
   
    _actionLabel.center = CGPointMake(200, 200);
   
    [UIView commitAnimations];
   
}


@end

5.執行結果,讓一個Label往另一個Label移動,使用Button(依序)來控制移動模式。

沒有留言: