IOS Programming Views: Redrawing and UIScrollView, iosuiscrollview

Source: Internet
Author: User

IOS Programming Views: Redrawing and UIScrollView, iosuiscrollview

IOS Programming Views: Redrawing and UIScrollView

1.1 event

You are going to see how views are redrawn in response to an event.

You will see how the view responds to the event.

You declared properties in header files. You can also declare properties in class extensions.

You can declare attributes in the header file or in class extensions.

# Import "BNRHypnosisView. h"

@ Interface BNRHypnosisView ()

@ Property (strong, nonatomic) UIColor * circleColor;

@ End

@ Implementation BNRHypnosisView

 

These three lines of code are a class extension with one property declaration.

There are three lines of code in a class extension.

 

When the user touches a view, the view is sent the message touchesBegan: withEvent :.

When you touch a view, the view will send a message to tochesBegan: withEvent:

 

Override touchesBegan: withEvent: to change the circleColor property of the view to a random color.

Override the touchesBegan: withEvent Method

// When a finger touches the screen
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event

{
NSLog (@ "% @ was touched", self );

// 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;

UIColor * randomColor = [UIColor colorWithRed: red green: green

Blue: blue alpha: 1.0];

Self. circleColor = randomColor ;}

1.2 Run Loop and redrawing views Loop and redraw views

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.

When an iOS application is started, it starts to loop. Run loop's job is to listen to events, such as touch

When an 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 have completed, control returns to the run loop.

When an event occurs, run loop needs to find the appropriate handler method to process the event. This handler processes other methods and calls more methods. After all the methods are completed, the control goes back to the run loop.

When the run loop regains control, it checks a list of "dirty views"-views that need to be re-rendered based on what happened in the most recent round of event handling.

When the run loop gets control again, it will detect a series of dirty views-views based on what needs to be re-painted based on the recent event handling.

The run loop then sends the drawRect: message to the views in this list before all of the views in the hierarchy are composited together again.

Before all views are combined, run loop sends information to the drawRect method of the view in the list.

Batching the redrawing of views at the end of a run loop cycle prevents needlessly redrawing a view more than once if more than one of its properties is changed in a single event.

Batch Processing of redrawing of views at the end of the run loop saves a lot of time.

To get a view on the list of dirty views, you must send it the message setNeedsDisplay.

To add a view to the dirty views list, you should send setNeedsDisplay to the view.

The subclasses of UIView that are part of the iOS SDK send themselves setNeedsDisplay whenever their content changes.

The UIView subclass in iOS SDK sends setNeedsDisplay to itself, no matter when their content changes.

In custom UIView subclasses, like BNRHypnosisView, you must send this message yourself.

 

In BNRHypnosisView. m, implement a custom accessor for the circleColor property to send setNeedsDisplay to the view whenever this property is changed.

In our example, we set setNeedsDisplay in the set Method of accessor.

-(Void) setCircleColor :( UIColor *) circleColor

{
_ CircleColor = circleColor;

[Self setNeedsDisplay];}

1.3 Class Extensions

What is the difference between a property declared in a class extension and one declared in a header file?

Declare in class extension and in header file?

A class's header file is visible to other classes. that, in fact, is its purpose. A class declares properties and methods in its header file to advertise to other classes how they can interact with the class or its instances.

Variables declared in the header file can be called by other classes. This is also the purpose.

Properties and methods that are used internally by the class belong in a class extension.

Attributes or methods can be called in class extension only internally.

It is good practice to keep your header file as brief as it can be. This makes it easier for others to understand how they can use your class.

It is a good habit to keep the header file small. It makes it easier for others to understand your class.

 

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.

A class extension starts with @ interface, followed by a class name followed by an empty parentheses.

2.1 UIScrollView

Scroll views are typically used for views that are 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 the position of that rectangle on the subview.

Scroll views are used as views larger than screens. A scroll view draws a part of its subview. When you move your finger on the scroll view, the position of the rectangle on the subview is changed.

Thus, you can think of the scroll view as a viewing port that you can move around

Therefore, you can regard scroll view as a view port.

The size of the scroll view is the size of this viewing port.

The size of the scroll view is the part of the view you see.

The size of the area that it can be used to view is the UIScrollView's contentSize, which is typically the size of the UIScrollView's subview.

You can use the contentSize of UIScrollView, that is, the size of the subview of UIScrollView.

// Create CGRects for frames
CGRect screenRect = self. window. bounds; CGRect bigRect = screenRect; bigRect. size. width * = 2.0; bigRect. size. height * = 2.0;

// Create a screen-sized scroll view and add it to the window

UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame: screenRect];

[Self. window addSubview: scrollView];

// Create a super-sized hypnosis view and add it to the scroll view

BNRHypnosisView * hypnosisView = [[BNRHypnosisView alloc] initWithFrame: bigRect];

[ScrollView addSubview: hypnosisView];

// Tell the scroll view how big its content area is scrollView. contentSize = bigRect. size;

 

2.2 Panning and paging

Another use for a scroll view is panning between a number of view instances.

In addition, an application of scroll view is panning (which should be switched) view instances.

 

// Create a screen-sized hypnosis view and add it to the scroll view BNRHypnosisView * hypnosisView = [[BNRHypnosisView alloc] initWithFrame: screenRect];

 

To force the scroll view to snap its viewing port to one of the views, turn on paging for the scroll view in BNRAppDelegate. m.

Sometimes you don't want to stop the graph in the middle. You can use pagingEnabled = yes;

UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame: screenRect]; scrollView. pagingEnabled = YES;
[Self. window addSubview: scrollView];

 

Paging works by taking the size of the scroll view's bounds and dividing up the contentSize it displays into sections of the same size.

Obtain the bounds of the scroll view by PAGE and divide the contentsize into segments of the same size.

 

After the user pans, the view port will scroll to show only one of these sections.

When the user switches back and forth, the view port only displays one of these segments.

 

Related Article

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.