iOS-使用两个不同的UIButton左右旋转图层 [英] iOS - Rotate a layer right and left using two different UIButtons

查看:96
本文介绍了iOS-使用两个不同的UIButton左右旋转图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CALayer创建了一个图层,并使用带有旋转动画的CABasicanimation对其进行了动画处理,并创建了两个TouchDown UIButtons来左右旋转它,当该图层向右旋转时,它又回到了原始角度,而与左相同时出现了问题旋转

I created a layer using CALayer and animated it using CABasicanimation with rotate animation and created two TouchDown UIButtons to rotate it left and right, I got a problem when the layer rotates right it gets back to the original angle and the same with left rotation

这是我的代码:

-(void)viewDidLoad
{
    //Button 1
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    aButton.frame = CGRectMake(0, 0, 145, 145);
    aButton.center = CGPointMake(90, 190);
    aButton.backgroundColor = [UIColor redColor];
    [aButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown];
    [aButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpOutside];
    [aButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:aButton];

    //Button 2
    UIButton *bButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    bButton.frame = CGRectMake(0, 0, 145, 145);
    bButton.center = CGPointMake(690, 190);
    bButton.backgroundColor = [UIColor redColor];
    [bButton addTarget:self action:@selector(holddDown) forControlEvents:UIControlEventTouchDown];
    [bButton addTarget:self action:@selector(holdReleasee) forControlEvents:UIControlEventTouchUpOutside];
    [bButton addTarget:self action:@selector(holdReleasee) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bButton];
}

-(void)holddDown{
    animaEQ = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animaEQ.toValue = [NSNumber numberWithFloat:5];
    animaEQ.removedOnCompletion = NO;
    animaEQ.repeatCount= HUGE_VAL;
    animaEQ.duration = 2.2f;
    [equaMi addAnimation:animaEQ forKey:@"blls"];
}
-(void)holdReleasee{
    [equaMi removeAnimationForKey:@"blls"];
}




- (void)holdDown
{
    animaEQl = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animaEQl.toValue = [NSNumber numberWithFloat:-5];
    animaEQl.removedOnCompletion = NO;
    animaEQl.repeatCount= HUGE_VAL;
    animaEQl.duration = 2.2f;
    [equaMi addAnimation:animaEQl forKey:@"bls"];
}

- (void)holdRelease
{
    [equaMi removeAnimationForKey:@"bls"];

}

请帮助吗?

感谢进阶

推荐答案

由于holdRelease方法删除了动画,因此恢复到原始角度

It's returning to its original angle because the holdRelease method removes the animation, reverting the layer's position.

使用CADisplay链接可以轻松地手动旋转图层。

Using a CADisplay link makes it easy to rotate the layer manually.

@implementation ViewController {
    CADisplayLink *timer;
}

-(void)viewDidLoad
{
    //Button 1
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    aButton.frame = CGRectMake(0, 0, 145, 145);
    aButton.center = CGPointMake(90, 190);
    aButton.backgroundColor = [UIColor redColor];
    aButton.tag = 0;
    [aButton addTarget:self action:@selector(holdDown:) forControlEvents:UIControlEventTouchDown];
    [aButton addTarget:self action:@selector(holdRelease:) forControlEvents:UIControlEventTouchUpOutside];
    [aButton addTarget:self action:@selector(holdRelease:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:aButton];

    //Button 2
    UIButton *bButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    bButton.frame = CGRectMake(0, 0, 145, 145);
    bButton.center = CGPointMake(690, 190);
    bButton.backgroundColor = [UIColor redColor];
    bButton.tag = 1;
    [bButton addTarget:self action:@selector(holdDown:) forControlEvents:UIControlEventTouchDown];
    [bButton addTarget:self action:@selector(holdRelease:) forControlEvents:UIControlEventTouchUpOutside];
    [bButton addTarget:self action:@selector(holdRelease:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bButton];
}

- (void)holdDown:(UIButton*)sender
{
    //check which button was pressed
    rotateLeft = (sender.tag == 0);
    //start the timer
    if (!timer) {
        timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(rotate)];
        timer.frameInterval = 1.0f;
        [timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    }
}

- (void)holdRelease:(UIButton*)sender
{
    //end the timer
    [timer invalidate];
    timer = Nil;
}

-(void)rotate
{
    //create the rotation transform based on current rotation and button pressed
    static int i;
    if (rotateLeft) {
        i++;
    } else {
        i--;
    }
    float rotation = (M_PI/60)*i;

    //disable implicit animations
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];

    //rotate the layer
    CGAffineTransform transform = CGAffineTransformMakeRotation(rotation);
    equaMi.affineTransform = transform;

    [CATransaction commit];
}

这篇关于iOS-使用两个不同的UIButton左右旋转图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆