iOS大典之点击旋转, 点击停止

最基本的旋转功能代码,

实现功能: 点击按钮开始旋转, 再次点击按钮停止, 再点击又开始旋转, 不点按钮不停止

注释有解释, 复制代码就可以实现功能, 很简单
代码如下:


1 . 声明需要用到的属性


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

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

2 . 创建方法,


- (void)addSubViews
{
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    self.imageView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.imageView];
    [self.imageView release];



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

先实现旋转功能, 再实现循环旋转功能, 注意要更改旋转状态,


// 点击就旋转,松开就停止

- (void)actionButton:(UIButton *)button
{
    // 输入animateWithDuration 提示中 选择托字符多的
    [UIView animateWithDuration:0.1 animations:^{

   // 设置旋转的角度
        self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_4);
    } completion:^(BOOL finished) {
        // 结束时调用旋转方法
    [self rorateAnimatetion];
    }];

   // 更改转动状态
     _isRuning = !_isRuning;

}


// 循环旋转

- (void)rorateAnimatetion
{
    if(_isRuning == YES){
        [UIView animateWithDuration:0.1 animations:^{

   // 设置旋转角度
     self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_4);

  } completion:^(BOOL finished) {

  // 旋转结束时调用旋转方法
        [self rorateAnimatetion];
        }];
    }
}

3 . 调用方法

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

    [self addSubViews];

}

over

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值