iOS开发之Scroll View Programming

Scroll View Programming


1. The UIScrollView class contains no specially defined view for the content that it displays; instead it simply scrolls its subviews. 

2. Adding basic pinch-in and pinch-out zoom support requires that the scroll view use delegation. 

Creating and Configuring Scroll Views

1. There are only two additional steps required to complete the scroll view configuration:
    (1)You must set the contentSize property to the size of the scrollable content. 
    (2)You must also add a view or views that are displayed and scrolled by the scroll view. 
    (3)You can optionally configure any visual cues your application requires—vertical and horizontal scroll indicators, drag bouncing, zoom bouncing, and directional constraint of scrolling

- (void)viewDidLoad {
    [super viewDidLoad];
    UIScrollView *tempScrollView=(UIScrollView *)self.view;
    tempScrollView.contentSize=CGSizeMake(1280,960);
}

2. Whether you create the scroll view in IB or programmatically, you are still responsible for setting the contentSize property in the view controller's viewDidLoad method. After the scroll view’s size has been configured, your application can then add the required subview(s) that provide the view content

Creating Scroll Views Programmatically


- (void)loadView {
    CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
    scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
    scrollView.contentSize=CGSizeMake(320,758);
 
    // do any further configuration to the scroll view
    // add a view, or views, as a subview of the scroll view.
 
    // release scrollView as self.view retains it
    self.view=scrollView;
    [scrollView release];
}

Configuring The Scroll View Content Size, Content Inset, And Scroll Indicators





1. To add padding to your application must set the contentInset property of the scroll view. The contentInset property specifies a buffer area around the content of the scroll view. The contentInset property is a UIEdgeInsets struct with the fields top, bottom, left, right. 

- (void)loadView {
    CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
    scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
    self.view=scrollView;
    scrollView.contentSize=CGSizeMake(320,758);
    scrollView.contentInset=UIEdgeInsetsMake(64.0,0.0,44.0,0.0);
 
    // do any further configuration to the scroll view
    // add a view, or views, as a subview of the scroll view.
 
    // release scrollView as self.view retains it
    self.view=scrollView;
    [scrollView release];
}

2. However, changing the contentInset value has an unexpected side effect when your scroll view displays scroll indicators. To correct this, you must set the scrollIndicatorInsets property.

- (void)loadView {
    CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
    scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
    scrollView.contentSize=CGSizeMake(320,758);
    scrollView.contentInset=UIEdgeInsetsMake(64.0,0.0,44.0,0.0);
    scrollView.scrollIndicatorInsets=UIEdgeInsetsMake(64.0,0.0,44.0,0.0);
 
    // do any further configuration to the scroll view
    // add a view, or views, as a subview of the scroll view.
 
    // release scrollView as self.view retains it
    self.view=scrollView;
    [scrollView release];

Scrolling the Scroll View Content


Scrolling Programmatically


1. Scrolling to a Specific Offset
Scrolling to a specific top-left location (the contentOffset property) can be accomplished in two ways. 
(1)The setContentOffset:animated: method scrolls the content to the specified content offset. The delegate is sent a scrollViewDidScroll: message.
(2) If animation is disabled, or if you set the content offset by setting the contentOffset property directly, the delegate receives a single scrollViewDidScroll: message. If animation is enabled, then the delegate receives a series of scrollViewDidScroll: messages as the animation is in progress. When the animation is complete, the delegate receives a scrollViewDidEndScrollingAnimation: message.

2. Making a rectangle visible
The scrollRectToVisible:animated: method scrolls the specified rectangle so that it is just visible inside the scroll view. If the animated parameter is YES, the rectangle is scrolled into view at a constant pace.

3. Scroll To Top
If the status bar is visible a scroll view can also scroll to the top of the content in response to a tap on the status bar. This practice is common in applications that provide a vertical representation of data. .

Your application enables this behavior by implementing the delegate method the scroll view property scrollViewShouldScrollToTop: and return YES. 


Delegate Messages Sent During Scrolling

 As scrolling occurs, the scroll view tracks state using the tracking, dragging, decelerating, and zooming properties. In addition, the contentOffset property defines the point in the content that is visible at the top left of the scroll view’s bounds.. The following table describes each of the state properties:

The Simple Approach: Tracking The Start and Completion Of A Scroll Action

   (1) Implement the scrollViewWillBeginDragging: method to receive notification that dragging will begin.
   (2) To determine when scrolling is complete you must implement two delegate methods:                  scrollViewDidEndDragging:willDecelerate: 
andscrollViewDidEndDecelerating:. 
Scrolling is completed either when the delegate receives the scrollViewDidEndDragging:willDecelerate: message with NO as the decelerate parameter, or when your delegate receives the scrollViewDidEndDecelerating: method. In either case, scrolling is complete.

The Complete Delegate Message Sequence


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值