开发者

(iphone) can i make UIScrollView to recognize horizontal dragging only?

I'm using UIScroll开发者_JS百科View's canceling touch ability with canCancelContentTouches.

However, I 'd like the uiscrollview to attempt to cancel touch when it detected horizontal dragging(not vertical).

(Hope solution would be available under < iOS 3.13)

Thank you


Implement the UIScrollViewDelegate and then use something like this:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [scrollView setContentOffset: CGPointMake(0, scrollView.contentOffset.y)];
}

Another way would be having a UIScrollView which is smaller or equal to the size of its parent view and with a disabled "Always bounce horizontal".


The safest and most successful method I've found to constrain the movement of a scroll view is to subclass UIScrollView and override setContentOffset:animated: and setContentOffset: methods (code below).

The advantage of overriding these methods is that it directly alters the requested contentOffset before any of the UIKit code starts to act on it, avoiding any of the side effects that can occur when modifying the contentOffset in scrollViewDidScroll: or other UIScrollViewDelegate methods.

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated {
    // restrict movement to horizontal only
    CGPoint newOffset = CGPointMake(contentOffset.x, 0);
    [super setContentOffset:newOffset animated:animated];
}

- (void)setContentOffset:(CGPoint)contentOffset {
    // restrict movement to horizontal only
    CGPoint newOffset = CGPointMake(contentOffset.x, 0);
    [super setContentOffset:newOffset];    
}


i guess i would use the method scrollViewWillBeginDragging found in the UIScrollViewDelegate and inside i could control if user is going horizontally or not...

scrollViewWillBeginDragging:

Tells the delegate when the scroll view is about to start scrolling the content.

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

Parameters

scrollView

The scroll-view object that is about to scroll the content view.

Discussion

The delegate might not receive this message until dragging has occurred over a small distance.

Availability Available in iOS 2.0 and later.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜