Tuesday, June 4, 2013

UIScrollView alwaysBounceHorizontal example in Objective C (iOS).

UIScrollView alwaysBounceHorizontal

A Boolean value that determines whether bouncing always occurs when horizontal scrolling reaches the end of the content view.

@property(nonatomic) BOOL alwaysBounceHorizontal

Discussion of [UIScrollView alwaysBounceHorizontal]
If this property is set to YES and bounces is YES, horizontal dragging is allowed even if the content is smaller than the bounds of the scroll view. The default value is NO.

UIScrollView alwaysBounceHorizontal example.
UIScrollView *scrollView;
CGSize size = scrollView.contentSize;
size.width = CGRectGetWidth(scrollView.frame);
scrollView.contentSize = size;
scrollView.alwaysBounceHorizontal = NO;

Example of [UIScrollView alwaysBounceHorizontal].
To appropriately make any UIScrollView always bounce, set its alwaysBounceHorizontal/Vertical: property:

[_scrollView setAlwaysBounceHorizontal:YES];

UIScrollView alwaysBounceHorizontal example.
- (void)viewDidLoad {
    [super viewDidLoad];

    m_pScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 768, 1024)];
    m_pScrollView.delegate = self;
    m_pScrollView.contentSize = CGSizeMake(768 * 3, 1024);
    m_pScrollView.pagingEnabled = YES;
    m_pScrollView.bounces = NO;
    m_pScrollView.alwaysBounceHorizontal = NO;
    m_pScrollView.alwaysBounceVertical = NO;

    Myview *myview = [[Myview alloc]initWithFrame:CGRectMake(0, 0, 768, 1024)];
    myview.backgroundColor = [UIColor redColor];

    Myview *myview2 = [[Myview alloc]initWithFrame:CGRectMake(768, 0, 768, 1024)];

    myview2.backgroundColor = [UIColor blueColor];
    Myview *myview3 = [[Myview alloc]initWithFrame:CGRectMake(768 * 2, 0, 768, 1024)];
    myview3.backgroundColor = [UIColor grayColor];

    [m_pScrollView addSubview:myview];
    [m_pScrollView addSubview:myview2];
    [m_pScrollView addSubview:myview3];
    [self.view addSubview:m_pScrollView];

End of UIScrollView alwaysBounceHorizontal example article.