IOS Programming views:redrawing and Uiscrollview

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">IOS Programming views:redrawing and uiscrollview?</span></p></p><p><p><span style="color:black; font-family:DFKai-SB"><span style="font-size:18pt">1.1 event?</span><span style="font-size:13pt"></span></span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">You is going to see what views is redrawn in response to an Event.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">You will see how the view responds to the Event.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">You declared properties in header Files. You can also declare the properties in class Extensions.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">You can declare a property in the header file, or you can declare it in class Extensions.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">#import "BNRHypnosisView.h"</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">@interface Bnrhypnosisview ()</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">@property (strong, Nonatomic) Uicolor *circlecolor;</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">@end</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">@implementation Bnrhypnosisview</span></p></p><p><p>?</p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">These three lines of code is a class extension with one property Declaration.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">There are three lines of code in a class Extension.</span></p></p><p><p>?</p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">When the user touches a view, The view is sent the message touchesbegan:withevent:.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">When the user touches a view, The view sends a message to Tochesbegan:withevent:</span></p></p><p><p>?</p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Override touchesBegan:withEvent:to Change the Circlecolor property of the view to a random Color.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Overriding the Touchesbegan:withevent method</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">When a finger touches the screen<br>-(void) touchesbegan: (nsset *) touches withevent: (uievent *) Event</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">{<br>NSLog (@ "%@ was touched", self);</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Get 3 random numbers between 0 and 1 float red = (arc4random ()% 100)/100.0; float green = (arc4random ()% 100)/100.0; float blue = (arc4random ()% 100)/100.0;</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Uicolor *randomcolor = [uicolor colorwithred:red green:green</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Blue:blue alpha:1.0];</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Self.circlecolor = randomcolor; }</span></p></p><p><p><span style="color:black; font-family:DFKai-SB"><span style="font-size:18pt">1.2 Run loop and redrawing views loop and redraw views</span><span style="font-size:13pt"></span></span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">When an IOS application is launched, It starts a run loop. The run loop ' s job is to listen for events, such as a Touch.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">When an iOS application starts, it starts Looping. Run Loop ' s job is to listen for events such as Touch</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">When a event occurs, the run loop then finds the appropriate handler methods for the Event. Those handler methods call other methods, which call more methods, and so On. Once all of the methods has completed, control returns to the run Loop.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">When the event occurs, run Loop will find the appropriate handler method to handle the Event. This handler handles other methods and calls more METHODS. Until all the methods have been completed, control is back to run Loop.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">When the run loop regains control, it checks a list of ' dirty views ' –views that need to being re-rendered based on what Ha Ppened in the recent round of event Handling.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">When run loop is regain control, it detects a series of dirty views-views according to the recent event handling what happened to REDRAW.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">The run loop then sends the drawrect:message to the "this" list before all of the views in the hierarchy is compo Sited together Again.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Before all of the view is re-assembled, the run loop sends a message to the DrawRect method of the view in the List.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt"></span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Batching the redrawing of views on the end of a run loop cycle prevents needlessly redrawing a view more than once if more than one of its properties are changed in a single event.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Batch processing redrawing of views at the end of the run loop, you can save a lot of Time.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">To get a view on the list of dirty views, you must send it the message setneedsdisplay.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">In order for an views to be added to the dirty views list, you should send Setneedsdisplay to the VIEW.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">The subclasses of UIView that is part of the IOS SDK send themselves setneedsdisplay whenever their content changes.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">The UIView subclasses within the iOS SDK will send themselves setneedsdisplay whenever their content changes.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">In the custom UIView subclasses, like bnrhypnosisview, You must send this message yourself.</span></p></p><p><p>?</p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">In bnrhypnosisview.m, implement a custom accessor for the Circlecolor property to send Setneedsdisplay to the view Wheneve R is Changed.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">In our example, we set the Setneedsdisplay in the accessor set Method.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">-(void) setcirclecolor: (uicolor *) circlecolor</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">{<br>_circlecolor = circlecolor;</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">[self setneedsdisplay]; }</span></p></p><p><p><span style="color:black; font-family:DFKai-SB"><span style="font-size:18pt">1.3 Class Extensions?</span><span style="font-size:13pt"></span></span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">What's the difference between a property declared in a class extension and one declared in a header file?</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Declare the class extension and in header file?</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">A class ' s header file is the visible to other Classes. that, in fact, was its purpose. A class declares properties and methods in their header file to advertise to other classes how they can interact with the CL or its instances.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">A variable declared in the header file can be called by another class. This is also its purpose.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Properties and methods that is used internally by the class belong in a class Extension.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">A property or method can be called in a class extension only if it is internal.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">It is good practice to keep your header file as brief as it Can. This makes it easier for others to understand how they can use your class.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">It's a good habit to keep the header file size Small. Make it easier for others to understand your class.</span></p></p><p><p>?</p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">A class extension looks a little like a header file. It begins with @interface followed by an empty set of Parentheses. The @end marks the end of the class Extension.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">A class extension @interface begins, followed by the class name, followed by an empty Parenthesis.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB"><span style="font-size:18pt">2.1 Uiscrollview</span><span style="font-size:13pt"></span></span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt"></span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Scroll views is typically used for views of that is larger than the Screen. A scroll view draws a rectangular portion of its subview, and moving your finger, or panning, on the scroll view changes T He position of that rectangle on the SUBVIEW.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Scroll views are used as those larger than the Screen. A scroll view painting part of its SUBVIEW. When you move your finger over the scroll view, the position of the rectangle on the subview is Changed.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Thus, you can think of the scroll view as a viewing port so you can move around</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Scroll view can therefore be viewed as a view port.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">The size of the scroll view is the size of this viewing port.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">The size of the scroll view is the part of the view that you see.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">The size of the area so it can be used to view are the Uiscrollview ' s contentsize, which is typically the size of the UIS Crollview ' s SUBVIEW.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">And you can use the size of the view is Uiscrollview contentsize, which is the size of Uiscrollview SUBVIEW.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Create Cgrects for Frames<br>CGRect Screenrect = self.window.bounds; CGRect Bigrect = screenrect; BigRect.size.width *= 2.0; BigRect.size.height *= 2.0;</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Create a screen-sized scroll view and add it to the window</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Uiscrollview *scrollview = [[uiscrollview alloc] initwithframe:screenrect];</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">[self.window addsubview:scrollview];</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Create a super-sized hypnosis view and add it to the scroll view</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Bnrhypnosisview *hypnosisview = [[bnrhypnosisview alloc] initwithframe:bigrect];</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">[scrollView addsubview:hypnosisview];</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">The scroll view how big it content area is Scrollview.contentsize = bigrect.size;</span></p></p><p><p>?</p></p><p><p><span style="color:black; font-family:DFKai-SB"><span style="font-size:18pt">2.2 Panning and paging?</span><span style="font-size:13pt"></span></span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Another use for a scroll view is panning between a number of view Instances.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Another application of scroll view is the panning (which should be the meaning of the Switch) view Instances.</span></p></p><p><p>?</p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Create a screen-sized Hypnosis view and add it to the scroll view bnrhypnosisview *hypnosisview = [[bnrhypnosisview All oc] [initwithframe:screenrect];</span></p></p><p><p>?</p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">To force the scroll view to snap their viewing port to one of the views, turn on paging for the scroll view in Bnrappdelegat e.m.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Sometimes you do not want the picture to stop in the middle position, you can use pagingenabled =yes;</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Uiscrollview *scrollview = [[uiscrollview alloc] initwithframe:screenrect]; scrollview.pagingenabled = YES;<br>[self.window addsubview:scrollview];</span></p></p><p><p>?</p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Paging works by taking the size of the scroll view ' s?bounds?and dividing up the?contentsize?it displays into sections of T He same size.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">Pagination is displayed as a sub-section of the same size by obtaining the bounds of the scroll view and dividing it into contentsize.</span></p></p><p><p>?</p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">After the user pans, the view port would scroll to show only one of the these Sections.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt">The view port displays only one of these sections when the user is flipping back and forth.</span></p></p><p><p><span style="color:black; font-family:DFKai-SB; font-size:13pt"></span>?</p></p><p><p>IOS Programming views:redrawing and Uiscrollview</p></p></span>

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.