Objective C:在 UIScrollView 上拖放
我正在尝试在 UIScrollView 上创建拖放功能,这对我来说很困惑,因为我是初学者。我尝试使用触摸事件,但失败了(尽管它适用于没有 UIScrollView 的项目)我读到了一些有关此问题的内容,他们建议使用手势识别器,所以我这么做了。
我的问题是这样的,我可以拖动它,但它被延迟了,我的意思是,我需要再次触摸图像才能在长按后拖动它......
我的长按手势上有这段代码:
-(void) handleLongPress:(UILongPressGestureRecognizer *)recognizer {
switch (recognizer.state) {
case UIGestureRecognizerStateBegan:
[img1 setFrame:CGRectMake(400, 385, 300, 300)];
[scrollPaging setScrollEnabled:NO];
[scrollPaging setUserInteractionEnabled:NO];
break;
这在touchesMoved上:
UITouch *touch =[[event allTouches] anyObject];
location=[touch locationInView:self.view];
img1.center=location;
return;
谢谢!
I am trying to make a drag and drop function on UIScrollView and it's quite confusing for me in my stage because i'm a beginner. i tried using touch event but i failed (though it worked on projects without UIScrollView) i read some about this issue and they suggested to use Gesture Recognizers, so i did.
My problem bow is this, i am able to drag it but it's delayed, i mean, i need to touch again the image to drag it after the longpress...
I have this code on my Longpress gesture:
-(void) handleLongPress:(UILongPressGestureRecognizer *)recognizer {
switch (recognizer.state) {
case UIGestureRecognizerStateBegan:
[img1 setFrame:CGRectMake(400, 385, 300, 300)];
[scrollPaging setScrollEnabled:NO];
[scrollPaging setUserInteractionEnabled:NO];
break;
And this on the touchesMoved:
UITouch *touch =[[event allTouches] anyObject];
location=[touch locationInView:self.view];
img1.center=location;
return;
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 UIPanGestureRecognizer 进行拖动,而不是 TouchMoved。还为平移和长按识别器设置委托并添加此方法:
Try using UIPanGestureRecognizer to drag instead of touchesMoved. Also set delegate for both pan and long press recognizers and add this method: