Tuesday, June 4, 2013

UIScrollView alwaysBounceVertical example in Objective C (iOS).


UIScrollView alwaysBounceVertical

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

@property(nonatomic) BOOL alwaysBounceVertical

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

UIScrollView alwaysBounceVertical example.
You can enable the scroll view to bounce using:

//scroll is an instance of UIScrollView
scroll.alwaysBounceVertical/Horizontal = YES;

Example of [UIScrollView alwaysBounceVertical].
            //create the cardViewScroller. this is where all the cards will be placed.
            cardScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
            cardScroller.backgroundColor = [UIColor clearColor];
            cardScroller.alwaysBounceVertical = NO;
            cardScroller.pagingEnabled = YES;
            cardScroller.showsVerticalScrollIndicator = NO;
            cardScroller.showsHorizontalScrollIndicator = NO;
            cardScroller.scrollEnabled = YES;   
            int cardscrollWidth = 120;
            cardScroller.contentSize = CGSizeMake(cardscrollWidth,80);

UIScrollView alwaysBounceVertical 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 alwaysBounceVertical example article.