UIScrollView,UIPageControl,uiscrollview

來源:互聯網
上載者:User

UIScrollView,UIPageControl,uiscrollview

 

 

 

#import <UIKit/UIKit.h>

 

@interface ViewController : UIViewController<UIScrollViewDelegate>

 

 

@end

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    //組成

    //上面 一個scrolview,一個addlable

    //中間一個pagecontrol

    //下面一個myScrollview一個remove按鈕

    

    

    self.view.backgroundColor=[UIColor greenColor];

 /***************UIScrollView*********************************/

    UIScrollView *scrolview=[[UIScrollView alloc]initWithFrame:CGRectMake(20, 20, 280, 180)];//高度等於圖片高度,寬度小於圖片寬度

    scrolview.backgroundColor=[UIColor blueColor];

    scrolview.contentSize=CGSizeMake(770, 180);//有效顯示範圍小於圖片,圖片不完全顯示

    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=@"啊,多麼美得一幅畫!";

    [scrolview addSubview:addlable];

    scrolview.pagingEnabled=YES;//分頁顯示的效果

    [scrolview setContentOffset:CGPointMake(250, 0) animated:YES];//載入後橫向滾動300像素

    scrolview.scrollEnabled=YES;//是否可以滾動的屬性

    scrolview.userInteractionEnabled=YES;//設定互動的屬性

    scrolview.showsHorizontalScrollIndicator=YES;

    scrolview.showsVerticalScrollIndicator=YES;//設定捲軸是否顯示

    scrolview.bounces=NO;//設定邊界的反彈效果,滾到邊界將不再滾動

    

//添加下面的myScrollview

    int weigth=700*280/1066;

    UIScrollView *myScrollview=[[UIScrollView    alloc]initWithFrame:CGRectMake(50, 250,weigth, 280)];//顯示框大小

    myScrollview.tag=121;

    myScrollview.backgroundColor=[UIColor redColor];

    myScrollview.contentSize=CGSizeMake(weigth*5, 280);//有效顯示地區

    myScrollview.delegate=self;//添加代理

//添加圖片組

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

        UIImageView *imageview1=[[UIImageView    alloc]initWithFrame:CGRectMake(weigth*i, 0, weigth, 280)];

        NSString *imageName=[NSString stringWithFormat:@"%d.jpg",i+1];

        imageview1.image=[UIImage imageNamed:imageName];

        [myScrollview addSubview:imageview1];

    }

    

//最後一頁添加一個按鈕

    UIButton *remove=[UIButton buttonWithType:UIButtonTypeCustom];

    remove.frame=CGRectMake(weigth*4+(weigth-100)/2, 220, 100, 35);//設定按鈕位置,可以隨頁面移動

    remove.backgroundColor=[UIColor orangeColor];

    [remove setTitle:@"開始體驗" forState:UIControlStateNormal];

    [remove addTarget:self action:@selector(doRemoveScrollView:) forControlEvents:UIControlEventTouchUpInside];//添加事件

    [myScrollview addSubview:remove];//添加按鈕

     myScrollview.pagingEnabled=YES;//按頁翻動

    [self.view addSubview:myScrollview];

    

//添加pagecontrol小白點,每個小白點一個頁數

    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];//添加事件

    pagecontrol.numberOfPages=5;//控制顯示的頁數

    pagecontrol.currentPage=0;//預設顯示第幾頁;

    [self.view addSubview:pagecontrol];

    

}

 

 

//每次scrollView變化後小白點位置變化

-(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];

}

//每次pagecontrol變動後scrollview都跟著變化

-(void)pageControlSelector:(UIPageControl *)sender{

  UIScrollView *getscroll=(UIScrollView   *)[self.view viewWithTag:121];//拿到scrollview;知道滾動對象

    CGSize size=getscroll.frame.size;

    CGRect reck1=CGRectMake((sender.currentPage *size.width), 0, size.width, size.height);

    [getscroll scrollRectToVisible:reck1 animated:YES];

}

//按鈕的方法

-(void)doRemoveScrollView:(id)sender{

    UIView *removeView=[self.view viewWithTag:121];

    [removeView removeFromSuperview ];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

 

 

}

 

@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.