首页 > 代码库 > UIScrollView的简单使用,可用来实现简单的相册
UIScrollView的简单使用,可用来实现简单的相册
MainViewController.h
#import <UIKit/UIKit.h> @interface MainViewController : UIViewController<UIScrollViewAccessibilityDelegate> @end
MainViewController.m
#import "MainViewController.h" @interface MainViewController () @end @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //UIScrollView的使用 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 280, 350)]; scrollView.backgroundColor = [UIColor brownColor]; // scrollView.alpha = 0.3; [self.view addSubview:scrollView]; [scrollView release]; //scorllView的滚动范围 scrollView.contentSize = CGSizeMake(840, 0); //scorllView能否滚动 scrollView.scrollEnabled = YES; //是否可以迅速回到顶部 scrollView.scrollsToTop = YES; //点击状态栏迅速回到顶部 //scorllView按页翻动 scrollView.pagingEnabled = YES; //边缘的弹动效果 scrollView.bounces = YES; //scrollView的偏移量 scrollView.contentOffset = CGPointMake(0, 0); //默认为CGPonintMake(0,0) //scrollView的代理 scrollView.delegate = self; //设置scrollView的缩放 //1.设置缩放范围 scrollView.maximumZoomScale = 2.0; scrollView.minimumZoomScale = 0.5; //创建存放imageView的可缩放的scrollView1 UIScrollView *scrollView1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 280, 350)]; scrollView1.delegate = self; scrollView1.maximumZoomScale = 2.0; scrollView1.minimumZoomScale = 0.5; [scrollView addSubview:scrollView1]; [scrollView1 release]; //创建存放imageView1的可缩放的scrollView2 UIScrollView *scrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(280, 0, 280, 350)]; scrollView2.delegate = self; // scrollView2.backgroundColor = [UIColor redColor]; scrollView2.maximumZoomScale = 2.0; scrollView2.minimumZoomScale = 0.5; [scrollView addSubview:scrollView2]; [scrollView2 release]; //创建存放imageView2的可缩放的scrollView3 UIScrollView *scrollView3 = [[UIScrollView alloc] initWithFrame:CGRectMake(560, 0, 280, 350)]; scrollView3.delegate = self; // scrollView2.backgroundColor = [UIColor redColor]; scrollView3.maximumZoomScale = 2.0; scrollView3.minimumZoomScale = 0.5; [scrollView addSubview:scrollView3]; [scrollView3 release]; //创建3个imageView 分别将它们放在scrollView1、scrollView2、scrollView3上面 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 350)]; UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 350)]; UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 350)]; imageView.image = [UIImage imageNamed:@"1.jpg"]; imageView1.image = [UIImage imageNamed:@"2.jpg"]; imageView2.image = [UIImage imageNamed:@"3.jpg"]; [scrollView1 addSubview:imageView]; [scrollView2 addSubview:imageView1]; [scrollView3 addSubview:imageView2]; [imageView2 release]; [imageView1 release]; [imageView release]; } //缩放的设置:2 指定一个scrollView的子视图,跟着scrollView缩放 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return [scrollView.subviews firstObject]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { //只要用户滚动scrollView,都会调用这个方法**************重要的 NSLog(@"任何偏移量的改变都会调用这个方法"); } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { //用户将要开始拖动scrollView的时候 NSLog(@"开始拖动"); } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { NSLog(@"结束拖动"); } - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView { NSLog(@"开始减速"); } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSLog(@"结束减速"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
本文出自 “小刘_Blog” 博客,请务必保留此出处http://liuyafang.blog.51cto.com/8837978/1548575
UIScrollView的简单使用,可用来实现简单的相册
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。