UIScrollView, UIPageControl, uiscrollview
# Import <UIKit/UIKit. h>
@ Interface ViewController: UIViewController <UIScrollViewDelegate>
@ End
# Import "ViewController. h"
@ Interface ViewController ()
@ End
@ Implementation ViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
// Composition
// The above scrolview and addlable
// A pagecontrol in the middle
// The following myScrollview remove button
Self. view. backgroundColor = [UIColor greenColor];
/************** UIScrollView *********************** **********/
UIScrollView * scrolview = [[UIScrollView alloc] initWithFrame: CGRectMake (20, 20,280,180)]; // The height equals to the image height and the width is less than the Image Width
Scrolview. backgroundColor = [UIColor blueColor];
Scrolview. contentSize = CGSizeMake (770,180); // The valid display range is smaller than the image, and the image is not completely displayed.
UIImageView * imageview = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0,770,180)];
Imageview. image = [UIImage imageNamed: @ "zhizhu"];
Imageview. backgroundColor = [UIColor yellowColor];
[Scrolview addSubview: imageview];
[Self. view addSubview: scrolview];
UILabel * addlable = [[UILabel alloc] initWithFrame: CGRectMake (310,150,160, 30)];
Addlable. text = @ "Ah, what a beautiful painting! ";
[Scrolview addSubview: addlable];
Scrolview. pagingEnabled = YES; // display results by PAGE
[Scrolview setContentOffset: CGPointMake (250, 0) animated: YES]; // scroll 300 pixels horizontally after loading
Scrolview. scrollEnabled = YES; // whether the attributes can be rolled
Scrolview. userInteractionEnabled = YES; // you can specify the interaction attribute.
Scrolview. showsHorizontalScrollIndicator = YES;
Scrolview. showsVerticalScrollIndicator = YES; // you can specify whether the scroll bar is displayed.
Scrolview. bounces = NO; // set the bounce effect of the Boundary. rolling to the boundary will not scroll.
// Add the following myScrollview
Int weigth = 700*280/1066;
UIScrollView * myScrollview = [[UIScrollView alloc] initWithFrame: CGRectMake (50,250, weigth, 280)]; // display the box size
MyScrollview. tag= 121;
MyScrollview. backgroundColor = [UIColor redColor];
MyScrollview. contentSize = CGSizeMake (weigth * 5,280); // valid display area
MyScrollview. delegate = self; // Add a proxy
// Add an image Group
For (int I = 0; I <5; I ++ ){
UIImageView * imageview1 = [[UIImageView alloc] initWithFrame: CGRectMake (weigth * I, 0, weigth, 280)];
NSString * imageName = [NSString stringWithFormat: @ "mongod.jpg", I + 1];
Imageview1.image = [UIImage imageNamed: imageName];
[MyScrollview addSubview: imageview1];
}
// Add a button on the last page
UIButton * remove = [UIButton buttonWithType: UIButtonTypeCustom];
Remove. frame = CGRectMake (weigth * 4 + (weigth-100)/2,220,100, 35); // set the button location that can be moved with the page
Remove. backgroundColor = [UIColor orangeColor];
[Remove setTitle: @ "start to experience" forState: UIControlStateNormal];
[Remove addTarget: self action: @ selector (doRemoveScrollView :) forControlEvents: UIControlEventTouchUpInside]; // Add an event
[MyScrollview addSubview: remove]; // Add button
MyScrollview. pagingEnabled = YES; // flip by PAGE
[Self. view addSubview: myScrollview];
// Add a pagecontrol point. Each point has a page number.
UIPageControl * pagecontrol = [[UIPageControl alloc] initWithFrame: CGRectMake (0,210,320, 30)];
Pagecontrol. tag= 122;
Pagecontrol. backgroundColor = [UIColor blueColor];
[Pagecontrol addTarget: self action: @ selector (pageControlSelector :) forControlEvents: UIControlEventValueChanged]; // Add an event
Pagecontrol. numberOfPages = 5; // control the number of displayed pages
Pagecontrol. currentPage = 0; // The default page number;
[Self. view addSubview: pagecontrol];
}
// The Position of the smallest vertex changes after each scrollView change
-(Void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView
{
UIPageControl * pagec = (UIPageControl *) [self. view viewWithTag: 122];
CGPoint point1 = scrollView. contentOffset;
NSLog (@ "% f", (float) point1.x );
[Pagec setCurrentPage: point1.x/183];
}
// The scrollview changes after each pagecontrol change.
-(Void) pageControlSelector :( UIPageControl *) sender {
UIScrollView * getscroll = (UIScrollView *) [self. view viewWithTag: 121]; // get scrollview; know the rolling object
CGSize size = getscroll. frame. size;
CGRect reck1 = CGRectMake (sender. currentPage * size. width), 0, size. width, size. height );
[Getscroll scrollRectToVisible: reck1 animated: YES];
}
// Button Method
-(Void) doRemoveScrollView :( id) sender {
UIView * removeView = [self. view viewWithTag: 121];
[RemoveView removeFromSuperview];
}
-(Void) didReceiveMemoryWarning {
[Super didReceiveMemoryWarning];
}
@ End