我正在尝试在iPhone应用中进行拖放。我有一个包含多层视图的主视图。
我放置了:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
在主视图中,但这不会被调用。当用户第一次触摸时,我能够在视图上加载缩略图图像。我尝试拖动UIImageView
,但touchesMoved:
未被调用。
这里有一些代码:(这是当用户触摸视图时加载的拖动图像,它加载并放置在光标下方)
[dragImage addTarget:self action:@selector(endDrag) forControlEvents:UIControlEventTouchUpOutside];
[dragImage addTarget:self action:@selector(endDrag) forControlEvents:UIControlEventTouchUpInside];
[dragImage addTarget:self action:@selector(endDrag) forControlEvents:UIControlEventTouchDragInside];
答案 0 :(得分:1)
这就是我在屏幕上拖动图像的方法:
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[imageView_ addGestureRecognizer:panRecognizer];
然后:
-(void)move:(id)sender {
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
}
答案 1 :(得分:1)
你可以做的是将UIImageView放在UIControl中(它将响应UIControlEventTouchDragInside)。
UIControl *dragImageControl = [[UIControl alloc] initWithFrame:dragImageView.frame];
[dragImageControl addSubview:dragImageView];
[self.view addSubview:dragImageControl];
[dragImageControl addTarget:self action:@selector(imageTouch:withEvent:) forControlEvents:UIControlEventTouchDown];
[dragImageControl addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
并且在imageMoved中,您可以拥有以下代码:
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
}