ios激情详解之动画旋转

13 篇文章 0 订阅
#import "RootViewController.h"

@interface RootViewController ()
@property (nonatomic, retain)UIImageView *imageView;
@property (nonatomic, assign) BOOL isRuning;

@end

@implementation RootViewController

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

- (void)addSubViews
{
    self.imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(80, 100, 200, 200))];
    self.imageView.backgroundColor = [UIColor redColor];
    self.imageView.image = [UIImage imageNamed:@"1.jpg"];
    
    [self.view addSubview:self.imageView];
    [self.imageView release];
    
    UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button1.frame = CGRectMake(200, 400, 40, 50);
    button1.backgroundColor = [UIColor blueColor];
    [button1 setTitle:@"点" forState:(UIControlStateNormal)];
    [button1 addTarget:self action:@selector(actionBlockButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button1];
    
}

// 点击方法 需求:点击按钮一直转,再点一下停
- (void)actionBlockButton:(UIButton *)button
{

#pragma mark -- 旋转
    [UIView animateWithDuration:0.1 animations:^{
        // 参数2:旋转角度
        self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_2);
    
        } completion:^(BOOL finished) {
        
        [self rotateAnimation];
    }];
    // 更改转动状态
    _isRuning =! _isRuning;
}
// 循环转:把转一次单独写个方法两个转互相调用,实现一直转
- (void)rotateAnimation
{
    if (_isRuning == YES) {
    [UIView animateWithDuration:0.1 animations:^{
        // 旋转
        // 参数2:旋转角度
        self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_4 * 4);
        
        // 给转一个初值
        _isRuning = YES;
        
    } completion:^(BOOL finished) {
        // 结束的时候调用循环转的方法
        [self rotateAnimation];
      }];
    }
}

// 点击方法
-(void)actionButton:(UIButton *)button
{
    // UIView动画:特点:全是类方法调用,开始与结束之间的部分 是动画改变的部分
    // 动画开始
    // 参数1:可以起一个标识符,参数2:携带的参数
    [UIView beginAnimations:@"UIView" context:nil];
 
    // 设置代理
    [UIView setAnimationDelegate:self];
    // 设置代理放法
    [UIView setAnimationWillStartSelector:@selector(WillStart)];
    [UIView setAnimationDidStopSelector:@selector(DidStop)];
    
    // 设置动画持续执行动画
    [UIView setAnimationBeginsFromCurrentState:YES];
    
    // 动画提交
    [UIView commitAnimations];
}

#pragma mark-- 自己设置的代理方法
- (void)WillStart
{
    NSLog(@"动画开始");
}
- (void)DidStop
{
    NSLog(@"动画结束");
  
}


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



@end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值