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控制項的簡單用法,希望對大家有所協助。