iphone- UIScrollview 对角线滑动意外行为

发布于 2024-12-13 14:43:52 字数 1593 浏览 0 评论 0原文

我正在开发一个应用程序,其中我必须从 UIScrollview 中拖动 UIImageview 。 在我的主 UIView 的底部,我有一个 UIScrollView,里面有 UIImageViews。 我使用以下代码片段创建了自定义 UIScrollview:

@interface CustomScroll : UIScrollView {

}@end


@implementation CustomScroll


- (id)initWithFrame:(CGRect)frame {
return [super initWithFrame:frame];

}

- (void) touchesMoved: (NSSet *) touches withEvent: (UIEvent *) event { 
// If not dragging, send event to next responder
if (!self.dragging) {
    self.scrollEnabled=NO;
    [self.nextResponder touchesMoved:touches withEvent:event]; 

}
else{
    [super touchesMoved: touches withEvent: event];
}

}

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event { 
// If not dragging, send event to next responder
if (!self.dragging)
    [self.nextResponder touchesEnded: touches withEvent:event]; 
else
    [super touchesEnded: touches withEvent: event];
}
@end

从这里开始,我在 PuzzleViewController 中包含了一个 CustomScroll 对象,并将其连接到界面生成器的滚动视图:

@interface PuzzleViewController : UIViewController <UIScrollViewDelegate> {
    IBOutlet CustomScroll *segmentScrollview;

}

我以编程方式添加了所有 UIImageview。

该代码按预期工作。 UIImageview 检测触摸,我可以将 UIImageview 从 UIScrollview 拖动到 UIView。

但是当我对角拖动 UIImageview 时会出现问题。

在我的应用程序中,当我从滚动视图垂直拖动 UIImageview 时,我可以将 UIImageView 从 UIScrollView 移动到 UIView。但是当我从滚动视图对角拖动 UIImageview 时,我无法移动 UIImageview。对角滑动滚动 UIScrollview [UIImageview 保持不变],这是意外的。我希望 UIImageview 应该从 UIScrollview 对角拖动。

任何帮助将不胜感激。

预先感谢您的任何帮助。

I am developing an app in which I have to drag UIImageview from UIScrollview.
At the bottom part of my main UIView, I've an UIScrollView with UIImageViews inside.
I have created custom UIScrollview using following code snippet:

@interface CustomScroll : UIScrollView {

}@end


@implementation CustomScroll


- (id)initWithFrame:(CGRect)frame {
return [super initWithFrame:frame];

}

- (void) touchesMoved: (NSSet *) touches withEvent: (UIEvent *) event { 
// If not dragging, send event to next responder
if (!self.dragging) {
    self.scrollEnabled=NO;
    [self.nextResponder touchesMoved:touches withEvent:event]; 

}
else{
    [super touchesMoved: touches withEvent: event];
}

}

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event { 
// If not dragging, send event to next responder
if (!self.dragging)
    [self.nextResponder touchesEnded: touches withEvent:event]; 
else
    [super touchesEnded: touches withEvent: event];
}
@end

From here I have included a CustomScroll object in my PuzzleViewController and connect it to the scrollview of Interface builder:

@interface PuzzleViewController : UIViewController <UIScrollViewDelegate> {
    IBOutlet CustomScroll *segmentScrollview;

}

I've added all UIImageviews programmatically.

This code is working as expected. UIImageview detecting touch and I can drag UIImageview from UIScrollview to UIView.

But problem occurs when I drag UIImageview diagonally.

In my app when I drag UIImageview vertically from scrollview I'm able to move my UIImageView from the UIScrollView to the UIView. But when I drag UIImageview diagonally from scrollview I can not move UIImageview. Diagonal swipe scrolls UIScrollview [UIImageview remains untouched] which is unexpected. I want UIImageview should be drag diagonally from UIScrollview.

Any help would be greatly appreciated.

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

反差帅 2024-12-20 14:43:52

这只是一个想法……所以尝试一下并告诉我效果如何。如果不起作用,那么我会尽力进一步帮助您。尝试设置 DirectionLockEnabled 为 NO。如果设置为“是”,那么您的移动将仅限于水平/垂直。如果它设置为“否”,那么您应该能够沿对角线移动。尝试一下并让我知道效果如何。

UIScrollView DirectionLockEnabled 文档

如果此属性为“否”,则允许水平滚动和滚动滚动。
垂直方向。 如果此属性为 YES 并且用户开始
沿一个大致方向(水平或垂直)拖动,
滚动视图禁用另一个方向的滚动。
如果拖动
方向是对角线,则滚动不会被锁定,用户
可以向任意方向拖动,直到拖动完成。默认值
是否

This is just a thought...so try it out and tell me how it goes. If doesn't work then I'll try and help you out further. Try setting directionalLockEnabled to NO. If it is set to YES then your movement will be restricted to horizontal/vertical only. If it is set to NO then you should be able to move diagonally. Try it out and let me know how it goes.

UIScrollView directionLockEnabled Docs

If this property is NO, scrolling is permitted in both horizontal and
vertical directions. If this property is YES and the user begins
dragging in one general direction (horizontally or vertically), the
scroll view disables scrolling in the other direction.
If the drag
direction is diagonal, then scrolling will not be locked and the user
can drag in any direction until the drag completes. The default value
is NO

眼眸印温柔 2024-12-20 14:43:52

我认为你所追求的不是 [super TouchesMoved:...],而是 [self.superview TouchsMoved:...] ...假设你有包含 CustomScroll 和你所在的其他视图的超级视图试图拖入设置来处理这个问题。

i am thinking that what you are after is not [super touchesMoved:...], but rather [self.superview touchesMoved:...] ... assuming you have the superview containing both the CustomScroll and the other view that you are attempting to drag into set up to deal with this.

半寸时光 2024-12-20 14:43:52

我知道这是一个较旧的问题,但我也遇到了这个问题。基本上我想要一个启用分页的滚动视图,它可以向上、向下、向左或向右移动。使用 pagingEnabled 和 orientationLockEnabled 变量使其基本上可以工作,除了对角线拖动之外。我的解决方案如下。

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.scrollView.contentOffset.x >= self.previousScrollOffset.x &&
        self.scrollView.contentOffset.y == self.scrollView.contentSize.height / 3){

        self.currentScrollDirection = SW_DIRECTION_LEFT;

    } else if (self.scrollView.contentOffset.x < self.previousScrollOffset.x &&
               self.scrollView.contentOffset.y == self.scrollView.contentSize.height / 3){

        self.currentScrollDirection = SW_DIRECTION_RIGHT;

    } else if (self.scrollView.contentOffset.x == self.scrollView.contentSize.width / 3 &&
         self.scrollView.contentOffset.y >= self.previousScrollOffset.y){

        self.currentScrollDirection = SW_DIRECTION_UP;

    } else if (self.scrollView.contentOffset.x == self.scrollView.contentSize.width / 3 &&
         self.scrollView.contentOffset.y <= self.previousScrollOffset.y){

        self.currentScrollDirection = SW_DIRECTION_DOWN;

    } else {

        self.currentScrollDirection = SW_DIRECTION_NONE;

    }

    //lock to either horizontal or vertical if we are dragging (prevent the diagonal drag)
    if (self.scrollView.isTracking && self.currentScrollDirection == SW_DIRECTION_NONE){
        self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height / 3);
    }

    self.previousScrollOffset = scrollView.contentOffset;
}

这会强制任何对角线滑动将滚动锁定在水平面上,这对我来说效果很好,因为水平拖动是优先级,所以向上或向下滚动的唯一方法是精确向上或向下滑动。

为此,您需要声明

@property (readwrite, nonatomic) SW_SCROLL_DIRECTION currentScrollDirection;
@property (readwrite, nonatomic) CGPoint previousScrollOffset;

typedef enum {
SW_DIRECTION_NONE,
SW_DIRECTION_LEFT,
SW_DIRECTION_RIGHT,
SW_DIRECTION_UP,
SW_DIRECTION_DOWN

} SW_SCROLL_DIRECTION;

笔记。这可能无法开箱即用,正如您所看到的,我硬编码了 3 页的值。这是因为我的内容始终居中,因此您可以向任何方向滚动一页,然后它会以动画方式返回到中心。修改以处理多个页面应该不难。

I know this is an older question, but I just ran into this problem as well. Basically I wanted a paging enabled scrollview which would go either up, down, left, or right. Using the pagingEnabled and directionalLockEnabled variables made it basically work, except for the diagonal drag. My solution is below.

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.scrollView.contentOffset.x >= self.previousScrollOffset.x &&
        self.scrollView.contentOffset.y == self.scrollView.contentSize.height / 3){

        self.currentScrollDirection = SW_DIRECTION_LEFT;

    } else if (self.scrollView.contentOffset.x < self.previousScrollOffset.x &&
               self.scrollView.contentOffset.y == self.scrollView.contentSize.height / 3){

        self.currentScrollDirection = SW_DIRECTION_RIGHT;

    } else if (self.scrollView.contentOffset.x == self.scrollView.contentSize.width / 3 &&
         self.scrollView.contentOffset.y >= self.previousScrollOffset.y){

        self.currentScrollDirection = SW_DIRECTION_UP;

    } else if (self.scrollView.contentOffset.x == self.scrollView.contentSize.width / 3 &&
         self.scrollView.contentOffset.y <= self.previousScrollOffset.y){

        self.currentScrollDirection = SW_DIRECTION_DOWN;

    } else {

        self.currentScrollDirection = SW_DIRECTION_NONE;

    }

    //lock to either horizontal or vertical if we are dragging (prevent the diagonal drag)
    if (self.scrollView.isTracking && self.currentScrollDirection == SW_DIRECTION_NONE){
        self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height / 3);
    }

    self.previousScrollOffset = scrollView.contentOffset;
}

This forces any diagonal swipes to lock the scrolling on the horizontal plane, which worked well for me because horizontal dragging was a priority, so The only way to scroll up or down would be to swipe exactly up or down.

For this to work you will need to declare

@property (readwrite, nonatomic) SW_SCROLL_DIRECTION currentScrollDirection;
@property (readwrite, nonatomic) CGPoint previousScrollOffset;

and

typedef enum {
SW_DIRECTION_NONE,
SW_DIRECTION_LEFT,
SW_DIRECTION_RIGHT,
SW_DIRECTION_UP,
SW_DIRECTION_DOWN

} SW_SCROLL_DIRECTION;

Note. This probably wont work out of the box, as you see I hardcode a value of 3 pages. This is because I have content always centred, so you can either scroll one page in any direction, then it will animate back to the centre. It shouldn't be to hard to modify to work with multiple pages.

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