gpt4 book ai didi

iOS - 使用两个不同的 UIButton 左右旋转图层

转载 作者:行者123 更新时间:2023-11-29 03:20:27 24 4
gpt4 key购买 nike

我使用 CALayer 创建了一个图层,并使用带有旋转动画的 CABasicanimation 对其进行了动画处理,并创建了两个 TouchDown UIButtons 来左右旋转它,当图层向右旋转时我遇到了一个问题,它回到了原来的角度,向左旋转时也是如此旋转

这是我的代码:

-(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方法删除了动画,恢复了图层的位置。

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

@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 左右旋转图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21170772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com