[iOS base Control-5.1] Uiscrollview

Source: Internet
Author: User

A. Need to Master Uiscrollview is a scroll-capable view control that can be used to display a large amount of content, such as the phone's "settings" 1. Common Properties 2. Common agent Methods 3. Scaling 4.UIScrollView and Uipagecontrol for paging 5.NSTime usage B.uiscrollview Concept and usage exercises: Display a large image, display a picture in its original size, and scroll through each part of the image: 1305 1. Drag into Uiscrollview, add a ImageView, set the picture, set the picture size Get the ScrollView and picture controls in the controller 2. Use the code to set the Contentsize property of the Uiscrollview, which is used to set the scroll range
1     // Set the scrolling range, here with the actual size of the picture 2     Self.scrollView.contentSize = self.imageView.frame.size;
3. If Uiscrollview cannot scroll, the possible reasons are as follows: (1) No Settings contentsize (2) scrollenable = no (3) No Touch event Received: Userinteractionenable = No (4) No cancel AutoLayout drag: 6.UIScrollView Common Properties
1     // indicates the scroll position, relative to the initial position 2     @property (nonatomic)         cgpoint                      contentoffset; 3    4     // representing dimensions, scrolling range 5     @property (nonatomic)         cgsize                       contentsize; 6    7     // This property can increase the additional scrolling range around 8     @property (nonatomic)         uiedgeinsets                 Contentinset;
(1) Various position, size: (2) extra margin Insets
1     // add margin display, parameters are top, left, bottom, right 2     Self.scrollView.contentInset = Uiedgeinsetsmake (ten);
(3) Moving Pictures
1     // move to (200, 300) position 2     Self.scrollView.contentOffset = Cgpointmake (a);
7. Other properties
1     //Do you need a spring effect ?2 @property (nonatomic) BOOL bounces;3    4     //is it possible to scroll5@property (nonatomic,getter=isscrollenabled) BOOL scrollenabled;6    7     //Show horizontal scroll bar8 @property (nonatomic) BOOL showshorizontalscrollindicator;9    Ten     //Show vertical scroll bar One@property (nonatomic) BOOL showsverticalscrollindicator;
 8. Agent Delegate(1) Use: Listen to the idea: let an object a listen to another object B State notification idea: An object B state has changed, to notify the object A is a B agent here, who want to listen, who is the agent (2) Use Uiscrollview internal delegate object, listen to events, achieve constant conversion of picture size
1 ID<UIScrollViewDelegate>      delegate//  default nil. Weak reference
(3) Uiscrollview Agent (delegate) Task:
    • Set Uiscrollview to do certain actions at a specific location, event, and events during scrolling
    • The entire scrolling process must be monitored
    • Sends a message (call method) proxy (delegate) object when scrolling occurs
    • So you need to set a delegate member for Uiscrollview.
So the delegate to be called Uiscrollview must implement the above methods, which are declared in the Uiscrollviewdelegate protocol, that is, to become the delegate of Uiscrollview, The Uiscrollviewdelegate agreement must be adhered to.(4) Set delegate three steps:A. Use the current controller directly as a delegate
1 #import <UIKit/UIKit.h>2@interface Viewcontroller:uiviewcontroller < Uiscrollviewdelegate>3@end
B. Designation of Uiscrollview delegate
1 Self.scrollview. delegate = self;
C.delegate Implementation Method I. For example, 3 methods for dragging and scrolling:
1- (void) Scrollviewwillbegindragging: (Uiscrollview *) ScrollView {2NSLog (@"Start dragging");3 }4 5- (void) Scrollviewdidscroll: (Uiscrollview *) ScrollView {6NSLog (@"being dragged");7 }8 9- (void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate {TenNSLog (@"End Drag"); One}
Ii. 3 Methods for gesture scaling
1- (void) Scrollviewwillbeginzooming: (Uiscrollview *) ScrollView Withview: (UIView *) View {2NSLog (@"Start Scaling");3 }4 5- (void) Scrollviewdidzoom: (Uiscrollview *) ScrollView {6NSLog (@"Scaling in Progress");7 }8 9- (void) Scrollviewdidendzooming: (Uiscrollview *) ScrollView Withview: (UIView *) View Atscale: (cgfloat) Scale {TenNSLog (@"End Zoom"); One}
Iii. deceleration after towing (2 methods)
 1 -(void ) Scrollviewwillbegindecelerating: (Uiscrollview *) ScrollView { 2  NSLog (@ "   "  3   4  5 -(void ) Scrollviewdidenddecelerating: (Uiscrollview *) ScrollView { 6  NSLog (@ "  Slow down after drag completion   "  7 } 
9. Gesture Zoom Uiscrollview not only scrolls through a large amount of content, but also scales its contents, so you only need to put the image you want to zoom into Uiscrollview. Only one child control can be scaled at a time, Uiscrollview returns the control that is specified to be scaled by using the delegate call gesture method
1     // methods for pinch gesture invocation 2     -(UIView *) Viewforzoominginscrollview: (Uiscrollview *) ScrollView;
This method returns the control that needs to be scaled
    • Set Minimumzoomscale: Minimum scale to zoom out
    • Set Maximumzoomscale: Maximum scale magnification
Implementation steps: (1) Implement gesture call method
1 // Image 2 @property (weak, nonatomic) iboutlet Uiimageview *ImageView; 3  4 // methods for scaling gesture calls 5 -(UIView *) Viewforzoominginscrollview: (Uiscrollview *) ScrollView {6      return//  need to enlarge ImageView7 }

(2) Setting the zoom ratio
1     2.0 ; 2     0.1;
(3) Effect A. Zoom out B. Enlarge 10. The pagination example is the Desktop app list, when the number of apps is more than one screen, automatically paging settings properties Pageenable = Yes,uiscrollview will be split into multiple separate pages, Paging displays generally use the Uipagecontrol enhancement effect, Uipagecontrol Common properties:
1     //Total Pages2@property (nonatomic) Nsinteger numberofpages;//default is 03     //Current page number4 @property (nonatomic) Nsinteger currentpage;5     //Hide page numbers only on one page6@property (nonatomic) BOOL hidesforsinglepage;//hide the The indicator if there is a one page. Default is NO7     //other page numbers indicate color8@property (nonatomic,retain) Uicolor *Pageindicatortintcolor;9     //Current page number indicates colorTen@property (Nonatomic,retain) Uicolor *currentpageindicatortintcolor;
11.NSTimer Timer, Function:
    • Performs the specified task at the specified time
    • Perform specified tasks at intervals
(1) Call the following method to turn on the scheduled task, and then call the fire method to execute:
1 + (Nstimer *) Timerwithtimeinterval: (nstimeinterval) ti target: (ID) atarget selector: (SEL) Aselector UserInfo: (ID) userInfo repeats: (BOOL) Yesorno; 2  3 -(void) fire;
The Aselector method is executed once per interval ti second, yesorno whether to repeat (2) Call method to create Nstimer, without calling fire will run
1 + (Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) ti invocation: (nsinvocation *) invocation Repeats: (BOOL) Yesorno;
(3) Through the Invalidate method can stop the work of the timer, but can not be executed again, can only re-create a new timer
1     // permanent termination of work 2     -(void) invalidate;
There are also two classes that can do a timed task:
    • Nstimer: Suitable for large time intervals
    • Cadisplaylink: Suitable for high frequency, do game animation
(4) The problem of a single-threaded timer UI interface can only use the main thread refresh, if the use of blocking means (such as Timer trigger screen slide, but can manually interfere with the screen movement), interference timer operation, will prevent the timer event triggered,    The blocked events accumulate after the thread resources are freed and then triggered. Workaround: When the blocking method prevents the timer from triggering, recycle the timer, and then re-create the timer after getting the thread resources.

[iOS base Control-5.1] Uiscrollview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.