Objective C:在 UIScrollView 上拖放

发布于 2024-12-28 12:04:32 字数 770 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双手揣兜 2025-01-04 12:04:32

尝试使用 UIPanGestureRecognizer 进行拖动,而不是 TouchMoved。还为平移和长按识别器设置委托并添加此方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES; }

Try using UIPanGestureRecognizer to drag instead of touchesMoved. Also set delegate for both pan and long press recognizers and add this method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文