1 Preface
When some content needs to be displayed on the screen, but the screen size cannot provide the required area, we need to consider using the UIScrollView control.
2. code example
ZYViewController. h:
[Plain] # import <UIKit/UIKit. h>
@ Interface ZYViewController: UIViewController <UIScrollViewDelegate>
@ Property (nonatomic, strong) UIImageView * myImageView;
@ Property (nonatomic, strong) UIScrollView * myScrollView;
@ End
# Import <UIKit/UIKit. h>
@ Interface ZYViewController: UIViewController <UIScrollViewDelegate>
@ Property (nonatomic, strong) UIImageView * myImageView;
@ Property (nonatomic, strong) UIScrollView * myScrollView;
@ End
ZYViewController. m:
[Plain] @ synthesize myImageView;
@ Synthesize myScrollView;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
UIImage * imageToLoad = [UIImage imageNamed: @ "Apple.png"];
Self. myImageView = [[UIImageView alloc] initWithImage: imageToLoad];
Self. myScrollView = [[UIScrollView alloc] initWithFrame: self. view. bounds]; // initialize the UIScrollerView Control
[Self. myScrollView addSubview: self. myImageView]; // Add the image to UIScrollerView.
Self. myScrollView. contentSize = self. myImageView. bounds. size; // set the image size to UIScrollerView.
Self. myScrollView. indicatorStyle = UIScrollViewIndicatorStyleWhite; // The sliding view is displayed in white.
MyScrollView. delegate = self; // set the proxy of the scroll view to itself.
[Self. view addSubview: self. myScrollView]; // Add UIScrollerView
}
# Pragma mark UIScrollView
// Call this method when the content slides
-(Void) scrollViewDidScroll :( UIScrollView *) scrollView {
Self. myScrollView. alpha = 0.5f;
NSLog (@ "Call scrollViewDidScroll method ");
}
// Slide view when the finger leaves the screen and the content is still sliding
-(Void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView {
Self. myScrollView. alpha = 1.0f;
NSLog (@ "Call scrollViewDidEndDecelerating method ");
}
// Called when the drag operation is complete
-(Void) scrollViewDidEndDragging :( UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {
Self. myScrollView. alpha = 1.0f;
NSLog (@ "Call scrollViewDidEndDragging method ");
}
@ Synthesize myImageView;
@ Synthesize myScrollView;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
UIImage * imageToLoad = [UIImage imageNamed: @ "Apple.png"];
Self. myImageView = [[UIImageView alloc] initWithImage: imageToLoad];
Self. myScrollView = [[UIScrollView alloc] initWithFrame: self. view. bounds]; // initialize the UIScrollerView Control
[Self. myScrollView addSubview: self. myImageView]; // Add the image to UIScrollerView.
Self. myScrollView. contentSize = self. myImageView. bounds. size; // set the image size to UIScrollerView.
Self. myScrollView. indicatorStyle = UIScrollViewIndicatorStyleWhite; // The sliding view is displayed in white.
MyScrollView. delegate = self; // set the proxy of the scroll view to itself.
[Self. view addSubview: self. myScrollView]; // Add UIScrollerView
}
# Pragma mark UIScrollView
// Call this method when the content slides
-(Void) scrollViewDidScroll :( UIScrollView *) scrollView {
Self. myScrollView. alpha = 0.5f;
NSLog (@ "Call scrollViewDidScroll method ");
}
// Slide view when the finger leaves the screen and the content is still sliding
-(Void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView {
Self. myScrollView. alpha = 1.0f;
NSLog (@ "Call scrollViewDidEndDecelerating method ");
}
// Called when the drag operation is complete
-(Void) scrollViewDidEndDragging :( UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {
Self. myScrollView. alpha = 1.0f;
NSLog (@ "Call scrollViewDidEndDragging method ");
}
Running result:
Before sliding:
Slide:
Console display content when dragging:
21:40:40. 586 UIScrollViewTest [676: c07] Call the scrollViewDidScroll Method
21:40:42. 986 UIScrollViewTest [676: c07] Call the scrollViewDidEndDragging Method
Console display content when sliding:
21:43:37. 789 UIScrollViewTest [676: c07] Call the scrollViewDidScroll Method
21:43:37. 789 UIScrollViewTest [676: c07] Call scrollViewDidEndDecelerating
Paging code:
ZYUIScrollViewController. h:
[Plain] # import <UIKit/UIKit. h>
@ Interface ZYUIScrollViewController: UIViewController
@ Property (nonatomic, strong) UIScrollView * myScrollView;
@ End
# Import <UIKit/UIKit. h>
@ Interface ZYUIScrollViewController: UIViewController
@ Property (nonatomic, strong) UIScrollView * myScrollView;
@ End
ZYUIScrollViewController. m:
[Plain] view plaincopyprint? -(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view.
Self. view. backgroundColor = [UIColor whiteColor];
// Instantiate three images
UIImage * iPhone = [UIImage imageNamed: @ "iPhone.png"];
UIImage * iPod = [UIImage imageNamed: @ "iPod.png"];
UIImage * macBookAir = [UIImage imageNamed: @ "Mac.png"];
CGRect scrollViewRect = self. view. bounds; // initialize a rectangular border with a screen view size.
Self. myScrollView = [[UIScrollView alloc] initWithFrame: scrollViewRect]; // initialize the UIScrollView Control
Self. myScrollView. pagingEnabled = YES; // start the page
Self. myScrollView. contentSize = CGSizeMake (scrollViewRect. size. width * 3.0f, scrollViewRect. size. height); // set the UIScrollView size
[Self. view addSubview: self. myScrollView]; // Add UIScrollView
CGRect imageViewRect = self. view. bounds;
UIImageView * iPhoneImageView = [[UIImageView alloc] initWithImage: iPhone]; // initialize the image View
[IPhoneImageView setFrame: imageViewRect]; // you can specify the UIImage size.
[Self. myScrollView addSubview: iPhoneImageView];
ImageViewRect. origin. x + = imageViewRect. size. width; // The horizontal coordinate of imageViewRect moves the distance from the original imageViewRect width to the right
UIImageView * iPodImageView = [[UIImageView alloc] initWithImage: iPod];
[IPodImageView setFrame: imageViewRect];
[Self. myScrollView addSubview: iPodImageView];
ImageViewRect. origin. x + = imageViewRect. size. width;
UIImageView * macBookAirImageView = [[UIImageView alloc] initWithImage: macBookAir];
[MacBookAirImageView setFrame: imageViewRect];
[Self. myScrollView addSubview: macBookAirImageView];
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view.
Self. view. backgroundColor = [UIColor whiteColor];
// Instantiate three images
UIImage * iPhone = [UIImage imageNamed: @ "iPhone.png"];
UIImage * iPod = [UIImage imageNamed: @ "iPod.png"];
UIImage * macBookAir = [UIImage imageNamed: @ "Mac.png"];
CGRect scrollViewRect = self. view. bounds; // initialize a rectangular border with a screen view size.
Self. myScrollView = [[UIScrollView alloc] initWithFrame: scrollViewRect]; // initialize the UIScrollView Control
Self. myScrollView. pagingEnabled = YES; // start the page
Self. myScrollView. contentSize = CGSizeMake (scrollViewRect. size. width * 3.0f, scrollViewRect. size. height); // set the UIScrollView size
[Self. view addSubview: self. myScrollView]; // Add UIScrollView
CGRect imageViewRect = self. view. bounds;
UIImageView * iPhoneImageView = [[UIImageView alloc] initWithImage: iPhone]; // initialize the image View
[IPhoneImageView setFrame: imageViewRect]; // you can specify the UIImage size.
[Self. myScrollView addSubview: iPhoneImageView];
ImageViewRect. origin. x + = imageViewRect. size. width; // The horizontal coordinate of imageViewRect moves the distance from the original imageViewRect width to the right
UIImageView * iPodImageView = [[UIImageView alloc] initWithImage: iPod];
[IPodImageView setFrame: imageViewRect];
[Self. myScrollView addSubview: iPodImageView];
ImageViewRect. origin. x + = imageViewRect. size. width;
UIImageView * macBookAirImageView = [[UIImageView alloc] initWithImage: macBookAir];
[MacBookAirImageView setFrame: imageViewRect];
[Self. myScrollView addSubview: macBookAirImageView];
}
Running result: