iOS base Control Uiscrollview

Source: Internet
Author: User
Tags uikit

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 scroll range, here with picture actual size 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 scroll position, relative to initial position 2     @property (nonatomic)         cgpoint                      contentoffset;3    4     //indicates dimensions, scroll range 5     @ Property (nonatomic)         cgsize                       contentsize;6    7     //This attribute can increase the additional scrolling range around 8     @property (nonatomic)         uiedgeinsets                 Contentinset;
(1) Various position, size: (2) extra margin Insets
1     //increase margin display, parameters are top, left, bottom, right2     self.scrollView.contentInset = Uiedgeinsetsmake (10, 20, 40, 80);
(3) Moving Pictures
1     //move to (200, 300) position 2     Self.scrollView.contentOffset = Cgpointmake (200, 300);
7. Other properties
1     //Whether need Spring Effect 2     @property (nonatomic)         BOOL                        bounces; 3     4     //Can scroll 5     @property (nonatomic, getter=isscrollenabled) bool          scrollenabled; 6     7     //Display horizontal scroll bar 8     @property (nonatomic)         bool                        Showshorizontalscrollindicator; 9     //Display vertical scrollbar 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 @property (nonatomic,assign) 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 {2      NSLog (@ "Start dragging"); 3} 4  5-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView {6     NSLog (@ "towing"); 7} 8  9-(void) Scrollviewdidenddragging: ( Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate {ten     NSLog (@ "End drag"); 11}
Ii. 3 Methods for gesture scaling
1-(void) scrollviewwillbeginzooming: (Uiscrollview *) ScrollView Withview: (UIView *) View {2     NSLog (@ "Start Scaling"); 3} 4
   5-(void) Scrollviewdidzoom: (Uiscrollview *) ScrollView {6     NSLog (@ "Scaling"); 7} 8  9-(void) Scrollviewdidendzooming: (Uiscrollview *) ScrollView Withview: (UIView *) View Atscale: (cgfloat) scale {ten     NSLog (@ ") End Scaling "); 11}
Iii. deceleration after towing (2 methods)
1-(void) scrollviewwillbegindecelerating: (Uiscrollview *) ScrollView {2     NSLog (@ "slow start after Towing"), 3}4 5-(void) Scrollviewdidenddecelerating: (Uiscrollview *) ScrollView {6     NSLog (@ "Slow down after towing complete"); 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     //Pinch gesture Call Method 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//Picture 2 @property (weak, nonatomic) Iboutlet uiimageview *imageview;3  4//Zoom gesture Call Method 5-(UIView *) VIEWFORZOOMINGINSCR Ollview: (Uiscrollview *) ScrollView {6     return self.imageview;//IMAGEVIEW7 needed to enlarge}

(2) Setting the zoom ratio
1     self.scrollView.maximumZoomScale = 2.0;2     Self.scrollView.minimumZoomScale = 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 pages 2     @property (nonatomic) Nsinteger numberofpages;          Default is 0 3     //Current page 4     @property (nonatomic) Nsinteger currentpage; 5     //Only one page hides page 6     @property (Nona Tomic) BOOL Hidesforsinglepage;          Hide the indicator if there is only one page. Default is NO 7     //Other page number indicates color 8     @property (nonatomic,retain) Uicolor *pageindicatortintcolor; 9     //Current page number indicates color 10     @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 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.