1 前言
當有的內容要顯示在屏幕上,但是屏幕的尺寸有提供不了所需的面積的時候,我們就要考慮用UIScrollView控件了。
2 代碼實例
ZYViewController.h:
[plain] #import <UIKit/UIKit.h>
@interface ZYViewController : UIViewController<UIScrollViewDelegate>
@property(nonatomic,strong) UIImageView *myImageView;
@property(nonatomic,strong) UIScrollView *myScrollView;
@end
#import <UIKit/UIKit.h>
@interface ZYViewController : UIViewController<UIScrollViewDelegate>
@property(nonatomic,strong) UIImageView *myImageView;
@property(nonatomic,strong) UIScrollView *myScrollView;
@end
ZYViewController.m:
[plain] @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方法");
}
@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:
[plain] #import <UIKit/UIKit.h>
@interface ZYUIScrollViewController : UIViewController
@property(nonatomic,strong) UIScrollView *myScrollView;
@end
#import <UIKit/UIKit.h>
@interface ZYUIScrollViewController : UIViewController
@property(nonatomic,strong) UIScrollView *myScrollView;
@end
ZYUIScrollViewController.m:
[plain] view plaincopyprint?- (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];
}
- (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];
}
運行結果: