当前位置:编程学习 > wap >>

IOS开发(18)之UIScrollView控件

1 前言
当有的内容要显示在屏幕上,但是屏幕的尺寸有提供不了所需的面积的时候,我们就要考虑用UIScrollView控件了。

2 代码实例
ZYViewController.h:

 

[plain]  #import <UIKit/UIKit.h> 
 
@inte易做图ce ZYViewController : UIViewController<UIScrollViewDelegate> 
 
@property(nonatomic,strong) UIImageView *myImageView; 
@property(nonatomic,strong) UIScrollView *myScrollView; 
 
@end 

#import <UIKit/UIKit.h>

@inte易做图ce 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> 
 
@inte易做图ce ZYUIScrollViewController : UIViewController 
 
@property(nonatomic,strong) UIScrollView *myScrollView; 
 
@end 

#import <UIKit/UIKit.h>

@inte易做图ce 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:ima

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,