IOS開發(18)之UIScrollView控制項

來源:互聯網
上載者:User
1 前言

當有的內容要顯示在螢幕上,但是螢幕的尺寸有提供不了所需的面積的時候,我們就要考慮用UIScrollView控制項了。

2 代碼執行個體

ZYViewController.h:

#import <UIKit/UIKit.h>@interface ZYViewController : UIViewController<UIScrollViewDelegate>@property(nonatomic,strong) UIImageView *myImageView;@property(nonatomic,strong) UIScrollView *myScrollView;@end

ZYViewController.m:

@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];//初始化UIScrollerView控制項    [self.myScrollView addSubview:self.myImageView];//添加圖片到UIScrollerView中    self.myScrollView.contentSize = self.myImageView.bounds.size;//將圖片的大小設定給UIScrollerView    self.myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滑動視圖外觀設定為白色    myScrollView.delegate = self;//滾動視圖的代理設定為自己    [self.view addSubview:self.myScrollView];//添加UIScrollerView}#pragma mark UIScrollView//內容滑動時調用該方法- (void)scrollViewDidScroll:(UIScrollView *)scrollView{    self.myScrollView.alpha = 0.5f;    NSLog(@"調用scrollViewDidScroll方法");}//滑動視圖手指離開螢幕而且內容還在滑動的時候-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    self.myScrollView.alpha = 1.0f;    NSLog(@"調用scrollViewDidEndDecelerating方法");}//完成拖拽動作時候調用的-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{    self.myScrollView.alpha = 1.0f;    NSLog(@"調用scrollViewDidEndDragging方法");}

運行結果:

滑動前:


滑動時候:


拖拽時候控制台顯示內容:

2013-04-25 21:40:40.586 UIScrollViewTest[676:c07]調用scrollViewDidScroll方法

2013-04-25 21:40:42.986 UIScrollViewTest[676:c07]調用scrollViewDidEndDragging方法

滑動時候控制台顯示內容:

2013-04-25 21:43:37.789 UIScrollViewTest[676:c07]調用scrollViewDidScroll方法

2013-04-25 21:43:37.789 UIScrollViewTest[676:c07]調用scrollViewDidEndDecelerating方法

分頁代碼:

ZYUIScrollViewController.h:

#import <UIKit/UIKit.h>@interface ZYUIScrollViewController : UIViewController@property(nonatomic,strong) UIScrollView *myScrollView;@end

ZYUIScrollViewController.m:

- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view.    self.view.backgroundColor = [UIColor whiteColor];    //執行個體化三張圖片    UIImage *iPhone = [UIImage imageNamed:@"iPhone.png"];    UIImage *iPod = [UIImage imageNamed:@"iPod.png"];    UIImage *macBookAir = [UIImage imageNamed:@"Mac.png"];    CGRect scrollViewRect = self.view.bounds;//初始化一個螢幕視圖大小矩形邊框    self.myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect];//初始化UIScrollView控制項    self.myScrollView.pagingEnabled = YES;//啟動分頁    self.myScrollView.contentSize = CGSizeMake(scrollViewRect.size.width*3.0f, scrollViewRect.size.height);//設定UIScrollView的大小    [self.view addSubview:self.myScrollView];//添加UIScrollView視圖    CGRect imageViewRect = self.view.bounds;    UIImageView *iPhoneImageView = [[UIImageView alloc] initWithImage:iPhone];//初始化圖片視圖    [iPhoneImageView setFrame:imageViewRect];//設定UIImage的大小    [self.myScrollView addSubview:iPhoneImageView];    imageViewRect.origin.x += imageViewRect.size.width;//imageViewRect的橫座標向右移動原來imageViewRect寬度的距離    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];    }

運行結果:




3 結語

以上就是UIScrollView控制項的簡單用法,希望對大家有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.