Using UIScrollView

In this section, you are going to add an instance of UIScrollView to Hypnosister. This scroll view will be a direct subview of the window, and the instance of BNRHypnosisView will be a subview of the scroll view, as shown in Figure 5.2.

Figure 5.2  View hierarchy with UIScrollView

View hierarchy with 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. Thus, you can think of the scroll view as a viewing port that you can move around (Figure 5.3). The size of the scroll view is the size of this viewing port. 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.

Figure 5.3  UIScrollView and its content area

UIScrollView and its content area

UIScrollView is a subclass of UIView, so it can be initialized using initWithFrame: and it can be added as a subview to another view.

In BNRAppDelegate.m, put a super-sized version of BNRHypnosisView inside a scroll view and add that scroll view to the window:

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
 ​ ​ ​ ​d​i​d​F​i​n​i​s​h​L​a​u​n​c​h​i​n​g​W​i​t​h​O​p​t​i​o​n​s​:​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​)​l​a​u​n​c​h​O​p​t​i​o​n​s​
{​
 ​ ​ ​ ​s​e​l​f​.​w​i​n​d​o​w​ ​=​ ​[​[​U​I​W​i​n​d​o​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​[​[​U​I​S​c​r​e​e​n​ ​m​a​i​n​S​c​r​e​e​n​]​ ​b​o​u​n​d​s​]​]​;​
 ​ ​ ​ ​/​/​ ​O​v​e​r​r​i​d​e​ ​p​o​i​n​t​ ​f​o​r​ ​c​u​s​t​o​m​i​z​a​t​i​o​n​ ​a​f​t​e​r​ ​a​p​p​l​i​c​a​t​i​o​n​ ​l​a​u​n​c​h​

 ​ ​ ​ ​C​G​R​e​c​t​ ​f​i​r​s​t​F​r​a​m​e​ ​=​ ​s​e​l​f​.​w​i​n​d​o​w​.​b​o​u​n​d​s​;​
 ​ ​ ​ ​B​N​R​H​y​p​n​o​s​i​s​V​i​e​w​ ​*​f​i​r​s​t​V​i​e​w​ ​=​ ​[​[​B​N​R​H​y​p​n​o​s​i​s​V​i​e​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​f​i​r​s​t​F​r​a​m​e​]​;​
 ​ ​ ​ ​[​s​e​l​f​.​w​i​n​d​o​w​ ​a​d​d​S​u​b​v​i​e​w​:​f​i​r​s​t​V​i​e​w​]​;​

 ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​C​G​R​e​c​t​s​ ​f​o​r​ ​f​r​a​m​e​s​
 ​ ​ ​ ​C​G​R​e​c​t​ ​s​c​r​e​e​n​R​e​c​t​ ​=​ ​s​e​l​f​.​w​i​n​d​o​w​.​b​o​u​n​d​s​;​
 ​ ​ ​ ​C​G​R​e​c​t​ ​b​i​g​R​e​c​t​ ​=​ ​s​c​r​e​e​n​R​e​c​t​;​
 ​ ​ ​ ​b​i​g​R​e​c​t​.​s​i​z​e​.​w​i​d​t​h​ ​*​=​ ​2​.​0​;​
 ​ ​ ​ ​b​i​g​R​e​c​t​.​s​i​z​e​.​h​e​i​g​h​t​ ​*​=​ ​2​.​0​;​

 ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​ ​s​c​r​e​e​n​-​s​i​z​e​d​ ​s​c​r​o​l​l​ ​v​i​e​w​ ​a​n​d​ ​a​d​d​ ​i​t​ ​t​o​ ​t​h​e​ ​w​i​n​d​o​w​
 ​ ​ ​ ​U​I​S​c​r​o​l​l​V​i​e​w​ ​*​s​c​r​o​l​l​V​i​e​w​ ​=​ ​[​[​U​I​S​c​r​o​l​l​V​i​e​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​s​c​r​e​e​n​R​e​c​t​]​;​
 ​ ​ ​ ​[​s​e​l​f​.​w​i​n​d​o​w​ ​a​d​d​S​u​b​v​i​e​w​:​s​c​r​o​l​l​V​i​e​w​]​;​

 ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​ ​s​u​p​e​r​-​s​i​z​e​d​ ​h​y​p​n​o​s​i​s​ ​v​i​e​w​ ​a​n​d​ ​a​d​d​ ​i​t​ ​t​o​ ​t​h​e​ ​s​c​r​o​l​l​ ​v​i​e​w​
 ​ ​ ​ ​B​N​R​H​y​p​n​o​s​i​s​V​i​e​w​ ​*​h​y​p​n​o​s​i​s​V​i​e​w​ ​=​ ​[​[​B​N​R​H​y​p​n​o​s​i​s​V​i​e​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​b​i​g​R​e​c​t​]​;​
 ​ ​ ​ ​[​s​c​r​o​l​l​V​i​e​w​ ​a​d​d​S​u​b​v​i​e​w​:​h​y​p​n​o​s​i​s​V​i​e​w​]​;​

 ​ ​ ​ ​/​/​ ​T​e​l​l​ ​t​h​e​ ​s​c​r​o​l​l​ ​v​i​e​w​ ​h​o​w​ ​b​i​g​ ​i​t​s​ ​c​o​n​t​e​n​t​ ​a​r​e​a​ ​i​s​
 ​ ​ ​ ​s​c​r​o​l​l​V​i​e​w​.​c​o​n​t​e​n​t​S​i​z​e​ ​=​ ​b​i​g​R​e​c​t​.​s​i​z​e​;​

 ​ ​ ​ ​s​e​l​f​.​w​i​n​d​o​w​.​b​a​c​k​g​r​o​u​n​d​C​o​l​o​r​ ​=​ ​[​U​I​C​o​l​o​r​ ​w​h​i​t​e​C​o​l​o​r​]​;​

Build and run your application. You can pan your view up and down, left and right to see more of the super-sized BNRHypnosisView.

Figure 5.4  Top right quadrant of big BNRHypnosisView

Top right quadrant of big BNRHypnosisView

When you go to pan around the BNRHypnosisView, the circle color changes. You cannot pan without beginning a touch, so the run loop sends touch events to the UIScrollView and to the BNRHypnosisView. In Chapter 13, you will see how to recognize and handle a tap gesture so that it can be distinguished from a touch or a drag.

Pinch-to-zoom is also implemented using UIScrollView. It does not take many lines of code, but it involves a technique that we have not covered yet. So adding pinch-to-zoom to Hypnosister will be a challenge in Chapter 7.

Panning and paging

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

In BNRAppDelegate.m, shrink the BNRHypnosisView back to the size of the screen and add a second screen-sized BNRHypnosisView as another subview of the UIScrollView. Set the scroll view’s contentSize to be twice as wide as the screen, but the same height.

/​/​ ​C​r​e​a​t​e​ ​C​G​R​e​c​t​s​ ​f​o​r​ ​f​r​a​m​e​s​
C​G​R​e​c​t​ ​s​c​r​e​e​n​R​e​c​t​ ​=​ ​s​e​l​f​.​w​i​n​d​o​w​.​b​o​u​n​d​s​;​
C​G​R​e​c​t​ ​b​i​g​R​e​c​t​ ​=​ ​s​c​r​e​e​n​R​e​c​t​;​
b​i​g​R​e​c​t​.​s​i​z​e​.​w​i​d​t​h​ ​*​=​ ​2​.​0​;​
b​i​g​R​e​c​t​.​s​i​z​e​.​h​e​i​g​h​t​ ​*​=​ ​2​.​0​;​

/​/​ ​C​r​e​a​t​e​ ​a​ ​s​c​r​e​e​n​-​s​i​z​e​d​ ​s​c​r​o​l​l​ ​v​i​e​w​ ​a​n​d​ ​a​d​d​ ​i​t​ ​t​o​ ​t​h​e​ ​w​i​n​d​o​w​
U​I​S​c​r​o​l​l​V​i​e​w​ ​*​s​c​r​o​l​l​V​i​e​w​ ​=​ ​[​[​U​I​S​c​r​o​l​l​V​i​e​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​s​c​r​e​e​n​R​e​c​t​]​;​
[​s​e​l​f​.​w​i​n​d​o​w​ ​a​d​d​S​u​b​v​i​e​w​:​s​c​r​o​l​l​V​i​e​w​]​;​

/​/​ ​C​r​e​a​t​e​ ​a​ ​s​u​p​e​r​-​s​i​z​e​d​ ​h​y​p​n​o​s​i​s​ ​v​i​e​w​ ​a​n​d​ ​a​d​d​ ​i​t​ ​t​o​ ​t​h​e​ ​s​c​r​o​l​l​ ​v​i​e​w​
B​N​R​H​y​p​n​o​s​i​s​V​i​e​w​ ​*​h​y​p​n​o​s​i​s​V​i​e​w​ ​=​ ​[​[​B​N​R​H​y​p​n​o​s​i​s​V​i​e​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​b​i​g​R​e​c​t​]​;​
/​/​ ​C​r​e​a​t​e​ ​a​ ​s​c​r​e​e​n​-​s​i​z​e​d​ ​h​y​p​n​o​s​i​s​ ​v​i​e​w​ ​a​n​d​ ​a​d​d​ ​i​t​ ​t​o​ ​t​h​e​ ​s​c​r​o​l​l​ ​v​i​e​w​
B​N​R​H​y​p​n​o​s​i​s​V​i​e​w​ ​*​h​y​p​n​o​s​i​s​V​i​e​w​ ​=​ ​[​[​B​N​R​H​y​p​n​o​s​i​s​V​i​e​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​s​c​r​e​e​n​R​e​c​t​]​;​
[​s​c​r​o​l​l​V​i​e​w​ ​a​d​d​S​u​b​v​i​e​w​:​h​y​p​n​o​s​i​s​V​i​e​w​]​;​

/​/​ ​A​d​d​ ​a​ ​s​e​c​o​n​d​ ​s​c​r​e​e​n​-​s​i​z​e​d​ ​h​y​p​n​o​s​i​s​ ​v​i​e​w​ ​j​u​s​t​ ​o​f​f​ ​s​c​r​e​e​n​ ​t​o​ ​t​h​e​ ​r​i​g​h​t​
s​c​r​e​e​n​R​e​c​t​.​o​r​i​g​i​n​.​x​ ​+​=​ ​s​c​r​e​e​n​R​e​c​t​.​s​i​z​e​.​w​i​d​t​h​;​
B​N​R​H​y​p​n​o​s​i​s​V​i​e​w​ ​*​a​n​o​t​h​e​r​V​i​e​w​ ​=​ ​[​[​B​N​R​H​y​p​n​o​s​i​s​V​i​e​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​s​c​r​e​e​n​R​e​c​t​]​;​
[​s​c​r​o​l​l​V​i​e​w​ ​a​d​d​S​u​b​v​i​e​w​:​a​n​o​t​h​e​r​V​i​e​w​]​;​

/​/​ ​T​e​l​l​ ​t​h​e​ ​s​c​r​o​l​l​ ​v​i​e​w​ ​h​o​w​ ​b​i​g​ ​i​t​s​ ​c​o​n​t​e​n​t​ ​a​r​e​a​ ​i​s​
s​c​r​o​l​l​V​i​e​w​.​c​o​n​t​e​n​t​S​i​z​e​ ​=​ ​b​i​g​R​e​c​t​.​s​i​z​e​;​

Build and run the application. Pan from left to right to see each instance of BNRHypnosisView. Notice that you can stop in between the two views.

Figure 5.5  In between the two hypnosis views

In between the two hypnosis views

Sometimes you want this, but other times, you do not. 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.

U​I​S​c​r​o​l​l​V​i​e​w​ ​*​s​c​r​o​l​l​V​i​e​w​ ​=​ ​[​[​U​I​S​c​r​o​l​l​V​i​e​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​s​c​r​e​e​n​R​e​c​t​]​;​
s​c​r​o​l​l​V​i​e​w​.​p​a​g​i​n​g​E​n​a​b​l​e​d​ ​=​ ​Y​E​S​;​
[​s​e​l​f​.​w​i​n​d​o​w​ ​a​d​d​S​u​b​v​i​e​w​:​s​c​r​o​l​l​V​i​e​w​]​;​

Build and run the application. Pan to the middle of two views and notice how it snaps to one or the other view. 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. After the user pans, the view port will scroll to show only one of these sections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset