UIScrollView的简单页面

#import "ViewController.h"

@interface ViewController ()

{

    UIImageView *imageview2;

    UIScrollView *sv;

    UIPageControl *page;

}

@end

@implementation ViewController

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    CGPoint newOff=scrollView.contentOffset;

    page.currentPage=newOff.x/self.view.frame.size.width+0.5;

 //   [sv addSubview:imageview2];

}

- (void)viewDidLoad {

    

    sv=[[UIScrollView alloc]initWithFrame:self.view.frame];

    sv.backgroundColor=[UIColor greenColor];

    sv.pagingEnabled=YES;

    sv.delegate=self;

    

   //UIScrollViewDelegate遵守了这个协议,然后实例化UIScrollview的对象

    NSString *str[]={@"D.jpg",@"oo.jpg",@"ll.jpg",@"u.jpg",@"liu.jpg"};

    page=[[UIPageControl alloc]initWithFrame:CGRectMake(50, self.view.frame.size.height-200, 200, 40)];

    [page addTarget:self action:@selector(changeSV) forControlEvents:UIControlEventValueChanged];

    page.numberOfPages=5;

    page.currentPage=0;

    

    page.currentPageIndicatorTintColor=[UIColor yellowColor];

    page.pageIndicatorTintColor=[UIColor greenColor];

    

    for (int i=0; i<5; i++)

    {

        UIImage *im=[UIImage imageNamed:[NSString  stringWithString:str[i]]];

        UIImageView *iv=[[UIImageView alloc]initWithImage:im];

        iv.frame=CGRectMake(i*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);

        if (i==3)

        {

            UITapGestureRecognizer *p=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(go )];

            [iv addGestureRecognizer:p ];

            iv.userInteractionEnabled=YES;

        }

              [sv addSubview:iv];

    }

     CGSize sz1=CGSizeMake(self.view.frame.size.width*5, self.view.frame.size.height);

    sv.contentSize=sz1;

    [self.view addSubview:sv];

    [self.view addSubview:page];

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

-(void)changeSV

{

    sv.contentOffset=CGPointMake(page.currentPage*self.view.frame.size.width, 0);

}

-(void)go

{

  //  [sv removeFromSuperview];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

原文地址:https://www.cnblogs.com/linximu/p/4409358.html