您的位置:首页 > 产品设计 > UI/UE

UIView动画

2015-09-23 20:00 411 查看
//
//  RootViewController.m
//  LessonUIView-01
//
//  Created by lanou3g on 15/9/23.
//  Copyright (c) 2015年 山神. All rights reserved.
//

#import "RootViewController.h"
#import "UIView+WLFrame.h"

@interface RootViewController ()

// 声明一个imageView
@property (nonatomic, retain) UIImageView *imageView;

// 保存初始的中心点
@property (nonatomic, assign) CGPoint center;

// 声明一个形变属性记录一下
@property (nonatomic, assign) CGAffineTransform transform;

// 声明一个属性记录是否正在转
@property (nonatomic, assign) BOOL isRuning;

@end

@implementation RootViewController

- (void)dealloc
{
[_imageView release];
[super dealloc];
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

[self addSomeViews];

}

- (void)addSomeViews
{
self.imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)] autorelease];
self.imageView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.imageView];
self.center = self.imageView.center;
self.transform = self.imageView.transform;

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 250, 100, 50);
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(100, 350, 100, 50);
button1.backgroundColor = [UIColor redColor];
[button1 addTarget:self action:@selector(actionButtonBlock:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}

- (void)actionButton:(UIButton *)button
{
// UIView动画
// 特点: 全是类方法调用, 开始与结束之间的部分是动画改变的部分
// 动画大小, 位置, 颜色, 透明度等

// 动画开始:  参数1:标示符(名字); 参数2:携带参数
[UIView beginAnimations:@"UIView" context:nil];

// 设置动画
[UIView setAnimationDuration:0.5]; //设置时间:在多少秒钟完结动画
[UIView setAnimationDelay:0]; // 设置动画延迟
[UIView setAnimationRepeatAutoreverses:YES]; // 设置反转
// 设置代理
[UIView setAnimationDelegate:self];
// 设置代理方法
[UIView setAnimationWillStartSelector:@selector(WillStart)];
[UIView setAnimationDidStopSelector:@selector(DidStop)];

// 设置速度曲线
[UIView setAnimationCurve:UIViewAnimationCurveLinear];

// 循环次数
[UIView setAnimationRepeatCount:1];

// 设置持续动画
[UIView setAnimationBeginsFromCurrentState:YES];

self.imageView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0]; // 改变颜色, 透明度

self.imageView.center = CGPointMake(300, 300); // 改变位置
CGRect frame = self.imageView.frame;
frame.size = CGSizeMake(200, 200);
self.imageView.frame = frame;

button.userInteractionEnabled = NO;
button.tag = 111;

// 动画提交
[UIView commitAnimations];
}

// UIView动画的block方法
- (void)actionButtonBlock:(UIButton *)button
{
//    button.userInteractionEnabled = NO;

// block方法1
// UIView动画的block方法
[UIView animateWithDuration:1 animations:^{
// 执行动画
self.imageView.center = CGPointMake(300, 300);
}];

// block方法2
// 参数3:该block写动画结束后要干的事
[UIView animateWithDuration:1 animations:^{
// 执行动画
// 2D仿射变换 transform形变属性
// 平移
// 参数1:要形变的view的transform

//        [UIView setAnimationRepeatAutoreverses:YES]; // 如果添加反转属性, 就不用添加block了
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 100, 0);

} completion:^(BOOL finished) {
// 上面动画结束后触发(相当于代理方法的完成动画方法)
// 复原位置
[UIView animateWithDuration:1 animations:^{
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, -100, 0);
} completion:^(BOOL finished) {
button.userInteractionEnabled = YES;
}];
}];

#pragma mark - 缩放
[UIView animateWithDuration:0.5 animations:^{
// 缩放: 参数2和3:缩放的比例.
[UIView setAnimationRepeatAutoreverses:YES];
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, 2, 2);
} completion:^(BOOL finished) {
self.imageView.transform = self.transform;
button.userInteractionEnabled = YES;
}];

#pragma mark - 旋转

// 需求: 点击按钮一直转, 再点就停

[UIView animateWithDuration:0.00001 animations:^{
// 旋转  参数2:旋转的角度
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, 0.15);
} completion:^(BOOL finished) {
// 结束时调用循环转方法
[self rotateAnimation];
}];
self.isRuning = !self.isRuning;
}

// 循环转
- (void)rotateAnimation
{

// 旋转
// 参数2 旋转的角度
if (self.isRuning == YES) {
[UIView animateWithDuration:0.01 animations:^{
// 旋转
// 参数2 旋转的角度
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, 0.35);
// 给一个转的初值
self.isRuning = YES;

} completion:^(BOOL finished) {
[self rotateAnimation];
}];

}
}

#pragma mark - 实现代理方法
- (void)WillStart
{
NSLog(@"动画开始");
}

- (void)DidStop
{
NSLog(@"动画停止");
// 复原初始位置
self.imageView.center = self.center;
UIButton *button = (UIButton *)[self.view viewWithTag:111];
self.imageView.backgroundColor = [UIColor greenColor];
self.imageView.frame = CGRectMake(100, 100, 100, 100);
button.userInteractionEnabled = YES;

}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: