An explanation of iOS Learning--uiscrollview principles
Source: Internet
Author: User
<span id="Label3"></p><span class="link_title">an <span class="link_title">Explanation of iOS Learning--uiscrollview principles</span></span><span class="link_categories"><span class="link_categories">http://blog.csdn.net/yanfangjin/article/details/7898189</span></span><p><p>ScrollView</p></p><p><p></p></p><p align="LEFT"><p align="LEFT">Uiscrollview</p></p><p align="LEFT"><p align="LEFT">Uiscrollview in order to display more than one screen of content or more than you can put in Memory.</p></p><p align="LEFT"><p align="LEFT">Scroll View handles zoom out gestures for you, Uiscrollview implements these gestures, and handles the detection and response for YOU. The subcategories that need attention are uitableview and Uitextview (used to display a large amount of text). There is also a uiwebview, although that is not the direct subclass of uiscrollview, It is suitable for Uiscrollview to display Web content</p></p><p align="LEFT"><p align="LEFT">Contentsize is the width and height of content, Contentsize.width is the width of the content, contentsize.heght is the height, contentsize is a property of uiscrollview, it is a cgsize, is a schema defined by the core graphics, that defines the width and height of the content you can scroll, you can also Add additional areas that can scroll up and down. The first method is that you can do this by adding the size of the Content. Another dynamic choice is another attribute contentinset of uiscrollview, Contentinset increases the amount of content that you specify in contentsize to scroll up and down the upper and lower left and right areas contentinset.top and contentinset.buttom indicate the distance above and below respectively.</p></p><p><p></p></p><p align="LEFT"><p align="LEFT">In the scroll view, there is a property called Contentoffset that tracks the exact location of uiscrollview, and you can get and set it yourself, Contentoffset is the point in the Upper-left corner of your current visual content on the scroll view boundary.</p></p><p><p></p></p><p align="LEFT"><p align="LEFT">As you can see, the point in the Contentoffset content is not from the upper-left corner of the contentinset, but the upper-left corner of the content, at which point the contentoffset is positive, but sometimes negative, as shown:</p></p><p><p></p></p><p align="LEFT"><p align="LEFT">Use a ScrollView</p></p><p align="LEFT"><p align="LEFT">Create a Uiscrollview</p></p><p align="LEFT"><p align="LEFT">Cgrectframe = CGRectMake (0, 0, 200, 200);</p></p><p align="LEFT"><p align="LEFT">scrollview= [[uiscrollview alloc] initwithframe:frame];</p></p><p align="LEFT"><p align="LEFT">Add a child view (the frame can exceed the bounds of the scroll view)</p></p><p align="LEFT"><p align="LEFT">Frame= cgrectmake (0, 0, 500, 500);</p></p><p align="LEFT"><p align="LEFT">myimageview= [[uiimageview alloc] initwithframe:frame];</p></p><p align="LEFT"><p align="LEFT">[scrollviewaddsubview:myimageview];</p></p><p align="LEFT"><p align="LEFT">Set Content Size</p></p><p align="LEFT"><p align="LEFT">Scrollview.contentsize= cgsize (500,500);</p></p><p align="LEFT"><p align="LEFT"></p></p><p align="LEFT"><p align="LEFT">Extending the behavior of scroll View</p></p><p align="LEFT"><p align="LEFT">Applications usually need to know about the events of the roll graph</p></p><p align="LEFT"><p align="LEFT">When Scrolloffset changed,</p></p><p align="LEFT"><p align="LEFT">Drag start and end</p></p><p align="LEFT"><p align="LEFT">Start and end of deceleration</p></p><p align="LEFT"><p align="LEFT"></p></p><p align="LEFT"><p align="LEFT">Extending the behavior of scroll View through subclasses</p></p><p align="LEFT"><p align="LEFT">Create a subclass</p></p><p align="LEFT"><p align="LEFT">Rewrite some features and change behavior</p></p><p align="LEFT"><p align="LEFT">The controversy about this approach</p></p><p align="LEFT"><p align="LEFT">The logic and behavior of the application becomes part of the view itself, as you might have some custom roller logic, where you only care about a view control, but you want to reuse your scroll view in different places, if you have to subclass for each, You'll end up with a lot of different roller view subclasses and specific application logic in the VIEW.</p></p><p align="LEFT"><p align="LEFT">Writing a lot of subclasses is tedious, and you end up with a lot of separate views that can't be reused, whereas the view part of MVC is a point where views can be reused in different controllers and different schemas, and if we put all the logic in the view, it reduces Reusability.</p></p><p align="LEFT"><p align="LEFT">Your code becomes so tightly paired that it actually becomes part of the superclass, you can't extract it from uiscrollview, and then replace it with something else, if it's in your controller and part of the controller, then it's easier to change the way it works and rearrange parts of your Application.</p></p><p align="LEFT"><p align="LEFT"></p></p><p align="LEFT"><p align="LEFT">Extending the behavior of scroll View through delegation (common)</p></p><p align="LEFT"><p align="LEFT">Delegation is a separate object, protocol, OBJECTIVE-C protocol that defines a series of features that a delegate implements, and it creates a series of clear undo points where you can customize behavior and Appearance. It keeps a loose pairing between these objects, the view itself with the view controller or any other controller object, and delegation is not a direct subclass of the roller view, it is more loosely than the rigidly paired Subclasses.</p></p><br><p><p></p></p><p><p></p></p><p><p></p></p><p><p>First quote someone to analyze a good article, but also thank the Friend's share: http://www.cocoachina.com/iphonedev/sdk/2010/1224/2503.html</p></p><p><p>In the process of scrolling, we are actually modifying the Origin Coordinates. When the finger is touched, scroll view temporarily intercepts the touch event, using a timer. If the finger move event does not occur after the timer point, then scroll view sends tracking events to the clicked Subview. If a move event occurs before the timer point, then scroll view cancels tracking itself.</p></p><p><p>Subclasses can overload</p></p><p><p>TouchesShouldBegin:withEvent:inContentView: decide if you want to receive touch events<br>Pagingenabled: The value is yes automatically scrolls to the Subview boundary, the default is no<br>Touchesshouldcancelincontentview: This method is called when the tracking messages message is started to be sent to subview, deciding whether to send tracking messages messages to SUBVIEW. If return NO, send. YES is not Sent.<br>If the Cancancelcontenttouches property is no, This method is not called to affect how the scrolling gesture is Handled.</p></p><p><p>Scroll view also handles zoom and pan gestures, and to achieve zoom and pan, the delegate Viewforzoominginscrollview must be implemented:, scrollViewDidEndZooming:withView:atScale:<br>Two METHODS. In addition Maximumzoomscale and Minimumzoomscale two attributes or the Like.</p></p><p><p>Several properties are introduced</p></p><p><p>Tracking<br>The value is yes when there is no drag after touch, otherwise no</p></p><p><p>Zoombouncing<br>The value is yes when the content is enlarged to the maximum or minimum, otherwise NO</p></p><p><p>Zooming<br>The value is yes when scaling, otherwise NO</p></p><p><p>decelerating<br>When scrolling, the finger is released but continues to Scroll. This time is yes, other times NO</p></p><p><p>Decelerationrate<br>Set the deceleration rate after the finger release</p></p><p><p>Maximumzoomscale<br>A floating-point number that represents the maximum possible multiplier</p></p><p><p>Minimumzoomscale<br>A floating-point number that represents the smallest possible multiplier</p></p><p><p>Pagingenabled<br>A value of Yes automatically scrolls to the bounds of the SUBVIEW. Default is No</p></p><p><p>Scrollenabled<br>Decide if you can scroll</p></p><p><p>Delayscontenttouches<br>is a boolean, when the value is yes, the user touches the beginning, scroll view to delay a while to see if the user has the intention to Scroll. If scrolling, then capture the Touch-down event, otherwise it will not be captured. If the value is no, when the user touches the scroll view will immediately trigger touchesShouldBegin:withEvent:inContentView:, the default is Yes</p></p><p><p>Cancancelcontenttouches<br>When the value is yes, when the user touches and then does not move within a certain amount of time, ScrollView sends tracking events, and then the user moves the finger enough to trigger the scrolling event, ScrollView sends the touchescancelled: Withevent: to subview, then Scroview starts scrolling. If the value is No,scrollview sent tracking events, even if the user moves the finger, ScrollView will not Scroll.</p></p><p><p>Contentsize<br>The size of the content, that is, the size can be scrolled, the default is 0, no scrolling effect.</p></p><p><p>Showshorizontalscrollindicator<br>Whether to display a horizontal scroll bar when scrolling</p></p><p><p>Showsverticalscrollindicator<br>Whether to display a vertical scroll bar when scrolling</p></p><p><p>Bounces<br>The default is yes, which means that scrolling beyond the bounds bounces back. If it is NO, then scrolling to reach the border will stop Immediately.</p></p><p><p>Bounceszoom<br>Similar to bounces, the difference is that this effect is reflected in the scale above, if the scale exceeds the maximum scale, then the effect will rebound, if NO, then the maximum or minimum time to stop immediately.</p></p><p><p>Directionallockenabled<br>The default is NO, which can be simultaneously moving vertically and horizontally. When the value is yes, if the start is vertical or horizontal movement, then the next one will lock the scroll in another Direction. If you start by scrolling diagonally, a direction is not forbidden</p></p><p><p>Indicatorstyle<br>The style of the scroll bar, basically just setting the Color. Total 3 colors: default, black, White</p></p><p><p>Scrollindicatorinsets<br>Set the position of the scroll bar</p></p><p><p>Finally add, I've just set an example that can be scaled and annotated with the proxy method used</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Scrollviewviewcontroller.m</p></p><p><p>ScrollView</p></p><p><p>//</p></p><p><p>Created by Ch_soft on 11-11-8.</p></p><p><p>Copyright 2011 __mycompanyname__. All Rights Reserved.</p></p><p><p>//</p></p><p><p></p></p><p><p>#import "ScrollViewViewController.h"</p></p><p><p>#import "CustomA.h"</p></p><p><p>#import "CustomB.h"</p></p><p><p></p></p><p><p>@implementation Scrollviewviewcontroller</p></p><p><p></p></p><p><p>-(void) didreceivememorywarning</p></p><p><p>{</p></p><p><p>Releases the view if it doesn ' t has a superview.</p></p><p><p>[super didreceivememorywarning];</p></p><p><p></p></p><p><p>Release any cached data, images, Etc-aren ' t in Use.</p></p><p><p>}</p></p><p><p></p></p><p><p>#pragma mark-view lifecycle</p></p><p><p></p></p><p><p></p></p><p><p>Implement Viewdidload to does additional setup after loading the view, typically from a nib.</p></p><p><p>-(void) Viewdidload</p></p><p><p>{</p></p><p><p>ScrollView =[[uiscrollview alloc] initwithframe:cgrectmake (50, 0, 200, 400)];</p></p><p><p>Scrollview.contentsize=cgsizemake (400, 400);</p></p><p><p>Scrollview.backgroundcolor=[uicolor orangecolor];</p></p><p><p>Scrollview.pagingenabled=yes;//whether to adapt oneself</p></p><p><p></p></p><p><p>Viewa=[[customa alloc] initwithframe:cgrectmake (50, 0, 100, 400)];</p></p><p><p>Viewa.backgroundcolor=[uicolor bluecolor];</p></p><p><p>[scrollview addsubview:viewa];</p></p><p><p>[viewa release];</p></p><p><p></p></p><p><p>Customb * Viewb=[[customb alloc] initwithframe:cgrectmake (250, 0, 100, 400)];</p></p><p><p>Viewb.backgroundcolor=[uicolor yellowcolor];</p></p><p><p>[scrollview addsubview:viewb];</p></p><p><p>[VIEWB release];</p></p><p><p></p></p><p><p>[self.view addsubview:scrollview];</p></p><p><p>scrollview.maximumzoomscale=2.0;</p></p><p><p>scrollview.minimumzoomscale=0.5;</p></p><p><p>scrollview.decelerationrate=1;</p></p><p><p>scrollview.delegate=self;</p></p><p><p>Cancancelcontenttouches:yes-moves the finger enough to trigger a scrolling event, No-scrollview sends tracking events, even if the user moves the finger, ScrollView will not Scroll.</p></p><p><p>scrollview.cancancelcontenttouches=no;</p></p><p><p></p></p><p><p>When the value is yes, the user touches the beginning. to delay a moment, see if the user has intent to Scroll. If scrolling, then capture the Touch-down event, otherwise it will not be captured. If the value is no, when the user touches the scroll view will immediately trigger</p></p><p><p>scrollview.delayscontenttouches=yes;</p></p><p><p>[scrollview release];</p></p><p><p>[super viewdidload];</p></p><p><p>}</p></p><p><p></p></p><p><p></p></p><p><p>#pragma Mark Uiscrollviewdelegate</p></p><p><p>As long as it scrolls, it triggers.</p></p><p><p>-(void) scrollviewdidscroll: (uiscrollview *) scrollView;</p></p><p><p>{</p></p><p><p>NSLog (@ "scrollviewdidscroll");</p></p><p><p>NSLog (@ "contentoffset x is%f,yis%f", scrollview.contentoffset.x,scrollview.contentoffset.y);</p></p><p><p>}</p></p><p><p>Start dragging the view</p></p><p><p>-(void) scrollviewwillbegindragging: (uiscrollview *) scrollView;</p></p><p><p>{</p></p><p><p>NSLog (@ "scrollviewwillbegindragging");</p></p><p><p>}</p></p><p><p>Complete the drag and drop</p></p><p><p>-(void) scrollviewdidenddragging: (uiscrollview *) scrollView willdecelerate: (BOOL) decelerate;</p></p><p><p>{</p></p><p><p>NSLog (@ "scrollviewdidenddragging");</p></p><p><p>}</p></p><p><p>When it starts to spin down</p></p><p><p>-(void) scrollviewwillbegindecelerating: (uiscrollview *) scrollView;</p></p><p><p>{</p></p><p><p>NSLog (@ "scrollviewwillbegindecelerating");</p></p><p><p>}</p></p><p><p></p></p><p><p>When the deceleration is stopped, execution is performed when the hand Touches.</p></p><p><p>-(void) scrollviewdidenddecelerating: (uiscrollview *) scrollView;</p></p><p><p>{</p></p><p><p>NSLog (@ "scrollviewdidenddecelerating");</p></p><p><p>}</p></p><p><p>Scrolling animation is executed when the code changes, i.e. when the Setcontentoffset changes</p></p><p><p>-(void) scrollviewdidendscrollinganimation: (uiscrollview *) scrollView;</p></p><p><p>{</p></p><p><p>NSLog (@ "scrollviewdidendscrollinganimation");</p></p><p><p>}</p></p><p><p>Set a zoomed-in view, if Uiscrollview's subview</p></p><p><p>-(UIView *) viewforzoominginscrollview: (uiscrollview *) scrollView;</p></p><p><p>{</p></p><p><p>NSLog (@ "viewforzoominginscrollview");</p></p><p><p>Return viewa;</p></p><p><p>}</p></p><p><p>Call when zoom Out is complete</p></p><p><p>-(void) scrollviewdidendzooming: (uiscrollview *) scrollView withview: (UIView *) view atscale: (float) scale;</p></p><p><p>{</p></p><p><p>Viewa.frame=cgrectmake (50, 0, 100, 400);</p></p><p><p>NSLog (@ "scale between minimum and maximum. called after any ' bounce ' animations");</p></p><p><p>}//scale between minimum and maximum. Called after any ' bounce ' animations</p></p><p><p></p></p><p><p>If you are not completely scrolling to the top of the scroll view, you can tap the status bar, and that visual scroll view will always be scrolled to the top, which is the default behavior that you can use to return no to close it</p></p><p><p>-(BOOL) scrollviewshouldscrolltotop: (uiscrollview *) scrollView;</p></p><p><p>{</p></p><p><p>NSLog (@ "scrollviewshouldscrolltotop");</p></p><p><p>Return YES;</p></p><p><p>}</p></p><p><p></p></p><p><p>-(void) scrollviewdidscrolltotop: (uiscrollview *) scrollView;</p></p><p><p>{</p></p><p><p>NSLog (@ "scrollviewdidscrolltotop");</p></p><p><p>}</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>-(void) Viewdidunload</p></p><p><p>{</p></p><p><p>[super viewdidunload];</p></p><p><p>Release any retained subviews of the main view.</p></p><p><p>e.g. Self.myoutlet = nil;</p></p><p><p>}</p></p><p><p></p></p><p><p>-(BOOL) shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation</p></p><p><p>{</p></p><p><p>Return YES for supported orientations</p></p><p><p>return (interfaceorientation = = uiinterfaceorientationportrait);</p></p><p><p>}</p></p><p><p></p></p><p><p>@end</p></p><p><p>An explanation of iOS Learning--uiscrollview principles</p></p></span>
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