-1
因此,我創建了一個簡單的應用程序,當用戶長按按鈕時,它旋轉了CALayer
,並且當用戶從按鈕上移開他的手時,設置了CALayer
速度到0
,所以CALayer
停在最後一點,然後從同一點完成。iOS - 旋轉對象使用核心動畫和longpress UIButton
(已解決)我的第一個問題是,使按鈕執行長按操作需要時間。 「解決使用UIControlEventTouchDown
」
我的第二個問題是,我想要CALayer
停止在一個特定的角度旋轉。
我的第三個問題是,我想讓兩個UIButton
的一個向左旋轉,另一個向右旋轉。
這裏是我的代碼:
@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;
}
}
第二個問題我的意思是當它停在130度時停止旋轉圖層。感謝您的幫助 – AFB
3個問題中的1個已從您的幫助中解決,謝謝! – AFB