[英]IOS: moving subviews on parent view
我是IOS的新手。 在我的應用中,每次用戶繪制一個圓時,我都會創建一個新的圓子視圖。 創建圓后,我希望用戶能夠在父視圖上拖動圓。 如果用戶創建了一個以上的圓圈,我如何知道觸摸了哪個圓圈並將其拖動到屏幕上。
我已經成功實現了自己想要的目標。 我在circle
subview
覆蓋了touchmoved
方法,並在其中輸入以下行:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
self.center = [mytouch locationInView:self.superview];
}
現在,每個創建的circle
都可以與屏幕一起拖動。
您可以將平移手勢添加到要移動的位置:
// Add UIPanGestureRecognizer to each circle view u add and set a tag for each circle
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @selector(handlePan:)];
[view addGestureRecognizer: panRecognizer];
-(void) handlePan: (UIGestureRecognizer *)sender
{
UIPanGestureRecognizer *panRecognizer = (UIPanGestureRecognizer *)sender;
UIView *view = sender.view;
// You can get to know which circle was moved by getting tag of this view
if (panRecognizer.state == UIGestureRecognizerStateBegan ||
panRecognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint currentPoint = self.center;
CGPoint translation = [panRecognizer translationInView: self.superView];
view.center = CGPointMake(currentPoint.x + translation.x, currentPoint.y + translation.y);
[panRecognizer setTranslation: CGPointZero inView: self.view];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.