Objective-c Learning-uiscrollview Controls using

Source: Internet
Author: User



First, the knowledge point simple introduction



What is a 1.UIScrollView control?



(1) The screen of the mobile device? Small is extremely limited, so direct exhibition, the content of the user's eyes is also very limited



(2) When the exhibition shows more content than a screen, the user can scroll through gestures to see what's outside of the screen



(3) Ordinary UIView does not have scrolling function, can not display too much content



(4) Uiscrollview is a scrollable view control that can be used to show a lot of content, and can scroll to see all the content



(5) Example: "Settings" on the phone, other? Sample programs



Simple use of 2.UIScrollView



(1) Add the content that needs to show to Uiscrollview



(2) Set the Contentsize property of the Uiscrollview, tell Uiscrollview all the contents of the ruler, that is, to tell it the range of scrolling (how far to roll, where to roll to the end)



3. Properties



(1) Common properties:



1) @property (nonatomic) Cgpointcontentoffset; This property is used to display uiscrollview scrolling position



2) @property (nonatomic) cgsizecontentsize; This property is used to uiscrollview the contents of the table, scroll range (how far to roll)



3) @property (nonatomic) Uiedgeinsetscontentinset; This property is capable of adding additional scrolling areas for 4 weeks of Uiscrollview



(2) Other properties:



1) @property (nonatomic) BOOL bounces; Set Uiscrollview If spring effect is required



2) @property (nonatomic,getter=isscrollenabled) boolscrollenabled; Sets whether Uiscrollview can scroll



3) @property (nonatomic) BOOL showshorizontalscrollindicator; Do you have a display? Horizontal scroll bar



4) @property (nonatomic) BOOL showsverticalscrollindicator; Do you want to show the vertical scroll bar?



4. Attention Points



• If Uiscrollview cannot scroll, the following may be the reason:



(1) No setup contentsize



(2) scrollenabled = NO



(3) No Touch event Received: userinteractionenabled = no



(4) No cancellation of AutoLayout function (if you want to scrollview scrolling, you must cancel AutoLayout)






Ii. some notes on common properties of Uiscrollview



1. Examples of code used by attributes


1 #import "MJViewController.h"
 2 
 3 @interface MJViewController ()
 4 {
 5 // Create an attribute in the private extension
 6 UIScrollView * _scrollView;
 7}
 8 @end
 9 
10 @implementation MJViewController
11
12-(void) viewDidLoad
13 {
14 [super viewDidLoad];
15
16 // 1. Create UIScrollView
17 UIScrollView * scrollView = [[UIScrollView alloc] init];
18 scrollView.frame = CGRectMake (0, 0, 250, 250); // size in frame refers to the visible range of UIScrollView
19 scrollView.backgroundColor = [UIColor grayColor];
20 [self.view addSubview: scrollView];
twenty one     
22 // 2. Create UIImageView (picture)
23 UIImageView * imageView = [[UIImageView alloc] init];
24 imageView.image = [UIImage imageNamed: @ "big.jpg"];
25 CGFloat imgW = imageView.image.size.width; // image width
26 CGFloat imgH = imageView.image.size.height; // height of the image
27 imageView.frame = CGRectMake (0, 0, imgW, imgH);
28 [scrollView addSubview: imageView];
29
30 // 3. Set the properties of scrollView
31
32 // Set the scroll range (content size) of UIScrollView
33 scrollView.contentSize = imageView.image.size;
34
35 // Hide the horizontal scroll bar
36 scrollView.showsHorizontalScrollIndicator = NO;
37 scrollView.showsVerticalScrollIndicator = NO;
38
39 // Used to record the scroll position of the scrollview
40 // scrollView.contentOffset =;
41
42 // remove the spring effect
43 // scrollView.bounces = NO;
44
45 // Add additional scroll area (counterclockwise, up, left, down, right)
46 // top left bottom right
47 scrollView.contentInset = UIEdgeInsetsMake (20, 20, 20, 20);
48
49 _scrollView = scrollView;
50}
51
52-(IBAction) down: (UIButton *) sender {
53 [UIView animateWithDuration: 1.0 animations: ^ {
54 // Three steps
55 CGPoint offset = _scrollView.contentOffset;
56 offset.y + = 150;
57 _scrollView.contentOffset = offset;
58
59 //_scrollView.contentOffset = CGPointMake (0, 0);
60}];
61}
62 @end 


2. Several attribute coordinates






3. Important Notes



(1) Uiscrollview frame and Contentsize attribute: Uiscrollview frame refers to the visual range of this ScrollView (visible area), Contentsize is its scroll range.



(2) Contentinset (usually not a struct is an enumeration), adding additional scrolling areas to the Uiscrollview. (Top, left, bottom, right) counterclockwise. Contentinset can be set using code or a view controller, but the two are different (note the distinction).



(3) The Contentsize property can only be set with code.



(4) Contentoffset is a cgpoint-type structure used to record the scroll position of the ScrollView, which is where the "box" is recorded. Knowing this property, you know where it is, and you can control the movement of the "box" by setting this property.



(5) It is not allowed to directly modify the members of an object's internal structure properties, three steps (get the value, modify it, and then assign the modified value back).



(6) Where is the origin of the Contentoffset when additional areas are added?



Iii. Some of the helpful understanding



Model Diagram:





Comparison chart:





Coordinate chart:






Objective-c Learning-uiscrollview Controls using


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.