我開發一個簡單的動畫,其中一個UIImageView
移動沿着UIBezierPath
,現在我想向用戶提供互爲作用到移動UIImageView
,使用戶可以通過觸摸UIImageView
並拖動引導UIImageView
屏幕周圍有UIImageview
。iPhone - 移動物體沿觸摸和拖拽路徑
4
A
回答
5
因此,您希望用戶能夠沿着彎曲路徑在關鍵幀動畫的中間觸摸圖像,並將其拖動到其他位置?那時你想要發生什麼動畫?
您有多重挑戰。
首先是在關鍵幀動畫處於「飛行中」時檢測對象上的觸摸。
爲此,您要使用父視圖的圖層的表示層的hitTest方法。
圖層的表示層代表任何給定時刻圖層的狀態,包括動畫。
一旦檢測到視圖上的接觸,您將需要從表示層獲取圖像的當前位置,停止動畫,並接管基於touchesMoved/touchesDragged的動畫。
我寫了一個演示應用程序,演示瞭如何檢測沿路徑動畫的對象的觸摸。這將是一個很好的起點。
到這裏看看:
Core Animation demo including detecting touches on a view while an animation is "in flight".
9
更改touchesMoved:withEvent:中圖像視圖位置的框架。
編輯:一些代碼
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [[event allTouches] anyObject];
if ([touch.view isEqual: self.view] || touch.view == nil) {
return;
}
lastLocation = [touch locationInView: self.view];
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [[event allTouches] anyObject];
if ([touch.view isEqual: self.view]) {
return;
}
CGPoint location = [touch locationInView: self.view];
CGFloat xDisplacement = location.x - lastLocation.x;
CGFloat yDisplacement = location.y - lastLocation.y;
CGRect frame = touch.view.frame;
frame.origin.x += xDisplacement;
frame.origin.y += yDisplacement;
touch.view.frame = frame;
lastLocation=location;
}
你也應該實現touchesEnded:withEvent:
和touchesCanceled:withEvent:
。
1
最簡單的方法是繼承UIImageView
。
對於簡單的拖動來看看這裏的代碼(代碼用戶MHC借來的):
既然你想沿着貝塞爾曲線拖你必須修改touchesMoved:
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *aTouch = [touches anyObject];
//here you have location of user's finger
CGPoint location = [aTouch locationInView:self.superview];
[UIView beginAnimations:@"Dragging A DraggableView" context:nil];
//commented code would simply move the view to that point
//self.frame = CGRectMake(location.x-offset.x,location.y-offset.y,self.frame.size.width, self.frame.size.height);
//you need some kind of a function
CGPoint calculatedPosition = [self calculatePositonForPoint: location];
self.frame = CGRectMake(calculatedPosition.x,calculatedPosition.y,self.frame.size.width, self.frame.size.height);
[UIView commitAnimations];
}
在-(CGPoint) calculatePositionForPoint:(CGPoint)location
究竟是你想要做什麼取決於你。例如,您可以計算最接近location
的Bezier路徑中的點。對於簡單的測試,你可以這樣做:
-(CGPoint) calculatePositionForPoint:(CGPoint)location {
return location;
}
除了你要去的方式必須決定是否用戶奇蹟從 預先計算貝塞爾路遠,會發生什麼關來。
相關問題
- 1. 沿着路徑移動通過觸摸
- 2. 沿Iphone路徑拖動
- 3. 在cocos2d中沿指定路徑觸摸並拖動精靈
- 4. 約束拖動沿着路徑在iphone
- 5. iPhone UITableView行拖拽移動
- 6. 沿線路徑拖動uiview
- 7. 沿着路徑拖動snap.svg
- 8. 沿着帶有java圖形的弧形路徑移動物體
- 9. SpriteKiit Swift:觸摸移動物體
- 10. 移動物體觸摸x y
- 11. 移動物體而不實際觸摸物體
- 12. 沿路徑移動形狀?
- 13. QGraphicsItem沿着路徑移動
- 14. 對象沿路徑移動
- 15. 沿路徑移動精靈
- 16. jQuery可拖拽+可拖拽 - 物理移動拖拽項目到被拖拽的容器
- 17. iOS - 沿彎曲路徑拖動對象
- 18. 沿傾斜/切線路徑拖動uiview
- 19. 沿着bezier路徑拖動UIView
- 20. 沿路徑拖動一個圓sgg
- 21. 在d3中沿路徑拖動
- 22. 沿預定路徑移動圖像?
- 23. iphone可可:如何沿着路徑拖動圖像
- 24. iPhone UIImageView與動畫圖像需要沿着路徑移動
- 25. 帶觸摸移動的拖動按鈕
- 26. UIImageView拖動和觸摸
- 27. Unity3d沿複雜曲線移動物體
- 28. 如何使box2d物體沿着貝塞爾曲線/圓弧路徑移動
- 29. iphone形象。多點觸摸/拖動
- 30. 在box2d中拖拽一個物體
先生,我有同樣的問題,就像你有請幫助我,如果你做那件事。 – 2014-06-11 06:50:07