2014-01-15 47 views
-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; 
} 
} 

回答

1

關於你的第一個問題,我不認爲你明白「長按」。當然,這需要時間來開始。這是設計。你不想使用它。看看觸摸點,並觸及行動。查看Apple文檔,或查找一些教程。我認爲你需要更好地理解基礎知識。

我認爲你應該首先解決這個問題,然後看看你是否能夠在使用正確的事件代碼後更容易地找出如何解決第三個問題。

至於你的第二個問題,我真的不明白你想完成什麼。什麼角度?你是否試圖抓住90/180/270/0度?請提供信息。

+0

第二個問題我的意思是當它停在130度時停止旋轉圖層。感謝您的幫助 – AFB

+0

3個問題中的1個已從您的幫助中解決,謝謝! – AFB