iOS中动画(一)——UIView的动画

iOS中UIView的动画

ViewController1.h

#import <UIKit/UIKit.h>

@interface ViewController1 : UIViewController

@property(nonatomic,retain)UIView* view1;

@end
ViewController1.m



#import "ViewController1.h"

@interface ViewController1 ()

@end

@implementation ViewController1

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
   
    
    _view1 = [[UIView alloc]init];
    _view1.frame = CGRectMake(0, 44, 160, 200);
    _view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:_view1];
    
    
        //过度动画的效果,是2个View的切换
    UIButton* button2 = [[UIButton alloc]initWithFrame:CGRectMake(20, 340, 150, 30)];
    [button2 addTarget:self action:@selector(button2) forControlEvents:UIControlEventTouchUpInside];
    [button2 setTitle:@"开始动画" forState:UIControlStateNormal];
    button2.backgroundColor = [UIColor redColor];
    [self.view addSubview:button2];

}
-(void)button2
{
    // 1 传统方法
    //开始动画
    /*[UIView beginAnimations:@"testanimation" context:nil];
    [UIView setAnimationDuration:0.5];
    //动画的代理
    [UIView setAnimationDelegate:self];
    //动画的响应事件,使视图自动回到原来的位置
    [UIView setAnimationDidStopSelector:@selector(animationstop)];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    //获得视图的位置
    CGRect frames = self.view1.frame;
    //将坐标移到右边160处
    frames.origin.x = 160;
    self.view1.frame = frames;
    [UIView commitAnimations];*/
    
    // 2 用block语法实现
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frames = self.view1.frame;
        frames.origin.x = 160;
        self.view1.frame = frames;
        //缩放
        self.view1.transform = CGAffineTransformScale(self.view1.transform, 0.01, 0.01);
    }completion:^(BOOL finished) {
        CGRect frames = self.view1.frame;
        frames.origin.x = 0;
        self.view1.frame = frames;
        //恢复到原始的缩放
        self.view1.transform = CGAffineTransformIdentity;
    }];
    
    
}
-(void)animationstop
{
    //传统方法
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    CGRect frames = self.view1.frame;
    frames.origin.x = 0;
    self.view1.frame = frames;
    [UIView commitAnimations];
}

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

@end



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静守晨昏‍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值