关闭

UIScrollView的简单使用

标签: uiscrollview
595人阅读 评论(0) 收藏 举报
#import <UIKit/UIKit.h>

@interface ScrollView : UIViewController<UIScrollViewDelegate>
{
    NSMutableArray * imageArray;
    NSTimer *timer;
    int pageNum;
}
@property (nonatomic,strong)UIScrollView *myScrollView;
@property (nonatomic,strong)UIImageView *myImageView;
@property (nonatomic,strong)UIPageControl *pageControl;
-(void)configScrollView;
-(void)initImageArray;
@end
#import "ScrollView.h"

@implementation ScrollView
@synthesize myImageView=_myImageView;
@synthesize myScrollView=_myScrollView;
@synthesize pageControl=_pageControl;

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self initImageArray];
    [self configScrollView];
     [self.view addSubview:self.myScrollView];
}
-(void)initImageArray
{

   imageArray =[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"1.jpg"],[UIImage imageNamed:@"2.jpg"],[UIImage imageNamed:@"3.jpg"],nil];
}
-(void)configScrollView  
{    
    CGRect scrollViewRect=self.view.bounds;
    self.myScrollView=[[UIScrollView alloc]initWithFrame:scrollViewRect];    
    self.myScrollView.delegate=self; 
     self.myScrollView.pagingEnabled=YES;
    
    UIImageView *firstView=[[UIImageView alloc] initWithImage:[imageArray lastObject]];  
    CGFloat Width=self.myScrollView.frame.size.width;  
    CGFloat Height=self.myScrollView.frame.size.height;  
    firstView.frame=CGRectMake(0, 0, Width, Height);  
    [self.myScrollView addSubview:firstView];  
    //set the last as the first  
    
    for (int i=0; i<[imageArray count]; i++) {  
        UIImageView *subViews=[[UIImageView alloc] initWithImage:[imageArray objectAtIndex:i]];  
        subViews.frame=CGRectMake(Width*(i+1), 0, Width, Height);  
        [self.myScrollView addSubview: subViews];  
    }  
    
    UIImageView *lastView=[[UIImageView alloc] initWithImage:[imageArray objectAtIndex:0]];  
    lastView.frame=CGRectMake(Width*(imageArray.count+1), 0, Width, Height);  
    [self.myScrollView addSubview:lastView];  
    //set the first as the last  
    
    [self.myScrollView setContentSize:CGSizeMake(Width*(imageArray.count+2), Height)];  
    [self.view addSubview:self.myScrollView];  
    [self.myScrollView scrollRectToVisible:CGRectMake(Width, 0, Width, Height) animated:NO];  
 
    CGRect pageControlFrame=CGRectMake(100, 160, 78, 36);  
    self.pageControl=[[UIPageControl alloc]initWithFrame:pageControlFrame];
    self.pageControl.numberOfPages=imageArray.count;   
    //    self.pageControl.backgroundColor=[UIColor blueColor];//背景  
    self.pageControl.currentPage=0;  
    self.pageControl.enabled=YES;  
    [self.view addSubview:self.pageControl];  
    [self.pageControl addTarget:self action:@selector(pageTurn:)forControlEvents:UIControlEventValueChanged];  
    
    timer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];  
    
} 
//前后各重复一张实现循环显示
-(void)scrollToNextPage:(id)sender
{
    pageNum=self.pageControl.currentPage;
    CGSize scrolViewSize=self.myScrollView.frame.size;
    CGRect rect=CGRectMake((pageNum+2)*scrolViewSize.width, 0, scrolViewSize.width, scrolViewSize.height);
    [self.myScrollView scrollRectToVisible:rect animated:NO];  
    pageNum++;
    if (pageNum==[imageArray count]) {
        CGRect newRect=CGRectMake(scrolViewSize.width, 0, scrolViewSize.width, scrolViewSize.height);
        [self.myScrollView scrollRectToVisible:newRect animated:NO];
    }
}

//拖动和分页
//- (void)viewDidLoad
//{
//    [super viewDidLoad];
//    UIImage *image1=[UIImage imageNamed:@"1.jpg"];
//    UIImage *image2=[UIImage imageNamed:@"2.jpg"];
//    UIImage *image3=[UIImage imageNamed:@"3.jpg"];
//    
//    CGRect scrollViewRect=self.view.bounds;
//    
//    self.myScrollView=[[UIScrollView alloc]initWithFrame:scrollViewRect];
//    self.myScrollView.pagingEnabled=YES;
//    
//    self.myScrollView.contentSize=CGSizeMake(scrollViewRect.size.width*3.0, scrollViewRect.size.height);
//    [self.view addSubview:self.myScrollView];
//    
//    CGRect imageViewRect=self.view.bounds;
//    UIImageView *imageView1=[[UIImageView alloc]initWithFrame:imageViewRect];
//    imageView1.image=image1;
//    [self.myScrollView addSubview:imageView1];
//    
//    imageViewRect.origin.x+=imageViewRect.size.width;
//    UIImageView *imageView2=[[UIImageView alloc]initWithFrame:imageViewRect];
//    imageView2.image=image2;
//    [self.myScrollView addSubview:imageView2];
//    
//    imageViewRect.origin.x+=imageViewRect.size.width;
//    UIImageView *imageView3=[[UIImageView alloc]initWithFrame:imageViewRect];
//    imageView3.image=image3;
//    [self.myScrollView addSubview:imageView3];
//}
//缩放
//-(void)viewDidLoad
//{
//    [super viewDidLoad];
//    CGRect scrollViewRect=self.view.bounds;
//    self.myScrollView=[[UIScrollView alloc]initWithFrame:scrollViewRect];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
//    self.myScrollView.contentSize=scrollViewRect.size;
//    [self.view addSubview:self.myScrollView];
//    
//    UIImage *image=[UIImage imageNamed:@"zoom.jpg"];
//    CGRect imageViewRect=CGRectMake(0, 0, image.size.width, image.size.height);
//    
//  self.myImageView=[[UIImageView alloc]initWithFrame:imageViewRect];
//    self.myImageView.image=image;
//    [self.myScrollView addSubview:self.myImageView];
//    
//    [self.myScrollView setMinimumZoomScale:1.0];
//    [self.myScrollView setMaximumZoomScale:5.0];
//    
//    [self.myScrollView setDelegate:self];
//}
//
//-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
//{
//    return self.myImageView;
//}
//floor(x)返回不大于x的最大整数        
-(void)scrollViewDidScroll:(UIScrollView *)scrollView  
{  
    CGFloat pageWidth=self.myScrollView.frame.size.width;   
    int currentPage=floor((self.myScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;  
    if (currentPage==0) {  
        self.pageControl.currentPage=imageArray.count-1;  
    }else if(currentPage==imageArray.count+1){  
        self.pageControl.currentPage=0;  
    }  
    self.pageControl.currentPage=currentPage-1;  
    
}  
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView  
{  
    [timer invalidate];  
}  
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate  
{  
    timer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];  
}  
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView  
{  
    CGFloat pageWidth=self.myScrollView.frame.size.width;  
    CGFloat pageHeigth=self.myScrollView.frame.size.height;  
    int currentPage=floor((self.myScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;  
    NSLog(@"the current offset==%f",self.myScrollView.contentOffset.x);  
    NSLog(@"the current page==%d",currentPage);  
    
    if (currentPage==0) {  
        [self.myScrollView scrollRectToVisible:CGRectMake(pageWidth*imageArray.count, 0, pageWidth, pageHeigth) animated:NO];  
        self.pageControl.currentPage=imageArray.count-1;  
        NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage);  
        NSLog(@"the last image");  
        return;  
    }else  if(currentPage==[imageArray count]+1){  
        [self.myScrollView scrollRectToVisible:CGRectMake(pageWidth, 0, pageWidth, pageHeigth) animated:NO];  
        self.pageControl.currentPage=0;  
        NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage);  
        NSLog(@"the first image");  
        return;  
    }  
    self.pageControl.currentPage=currentPage-1;  
    NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage);  
    
}  
-(IBAction)pageTurn:(UIPageControl *)sender  
{  
    int pageNum=self.pageControl.currentPage;  
    CGSize viewSize=self.myScrollView.frame.size;  
    [self.myScrollView setContentOffset:CGPointMake((pageNum+1)*viewSize.width, 0)];  
//    NSLog(@"myscrollView.contentOffSet.x==%f",myScrollView.contentOffset.x);  
//    NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage);  
    [timer invalidate];  
} 
@end




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:112382次
    • 积分:1835
    • 等级:
    • 排名:千里之外
    • 原创:64篇
    • 转载:48篇
    • 译文:6篇
    • 评论:10条
    最新评论