【发布时间】: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