【问题标题】:iOS - Rotating objects using core animation and longpress UIButtoniOS - 使用核心动画和长按 UIButton 旋转对象
【发布时间】:2014-01-15 12:43:04
【问题描述】:

所以,我创建了一个简单的应用程序,当用户长按按钮时它会旋转 CALayer,当用户将手从按钮上移开时,CALayer 速度设置为 0,因此CALayer 停在最后一点,然后从同一点完成。

(已解决)我的第一个问题是,使按钮执行长按操作需要时间。 "使用UIControlEventTouchDown解决"

我的第二个问题是,我希望 CALayer 停止以特定角度旋转。

我的第三个问题是,我想制作两个UIButtons 一个用于向左旋转,另一个用于向右旋转。

这是我的代码:

@interface anApp ()
{
CALayer *lyr;
}

@end
-(void)viewDidLoad{
//The layer
lyr.bounds = CGRectMake(0, 0, 110, 190);
lyr.position = CGPointMake(90, 190);
lyr.backgroundColor = [UIColor redColor].CGColor;
[self.view.layer addSubLayer:lyr];
//The rotate animation
CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
anima.toValue = [NSNumber numberWithFloat:9];
anima.removedOnCompletion = NO;
anima.repeatCount= HUGE_VAL;
anima.duration = 1.1f;
[lyr addAnimation:anima forKey:@"rota"];
//Set speed to zero
lyr.timeOffset = [lyr convertTime:CACurrentMediaTime() fromLayer:nil];
lyr.beginTime = CACurrentMediaTime();
lyr.speed = 0.0;

//Long press UIButton
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, theHeight/2, theWidth);
btn.center = CGPointMake(theHeight/1.33, theWidth/2);
[self.view addSubview:btn];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[btn addGestureRecognizer:longPress];
}

//The long press UIButton action
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
//if the user pressed a long press on a button
if(gesture.state == UIGestureRecognizerStateBegan){
//set the speed to 1
lyr.speed = 1.0;
CFTimeInterval pausedTime = lyr.timeOffset;
lyr.timeOffset = 0.0;
lyr.beginTime = 0.0;
CFTimeInterval timeSincePause = [lyr convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
lyr.beginTime = timeSincePause;
}

//if the user removed his/her finger from the button
if(gesture.state == UIGestureRecognizerStateEnded){
//set the speed of the layer to 0
lyr.timeOffset = [lyr convertTime:CACurrentMediaTime() fromLayer:nil];
lyr.beginTime = CACurrentMediaTime();
lyr.speed = 0.0;
}
}

【问题讨论】:

    标签: ios objective-c uibutton core-graphics core-animation


    【解决方案1】:

    关于您的第一个问题,我认为您不了解“长按”。当然,开始需要时间。这是设计使然。你不想用那个。看看触地得分和触地得分动作。查看 Apple 文档,或查找一些教程。我认为您需要更好地了解基础知识。

    我认为你应该先解决这个问题,然后看看你是否可以在使用正确的事件代码后更轻松地解决第三个问题。

    关于你的第二个问题,我真的不明白你想要完成什么。什么角度?您是否要捕捉到 90/180/270/0 度?请提供信息。

    【讨论】:

    • 第二个问题我的意思是当图层在 130 度时停止旋转。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多