UIScrollView的基本使用
// // ViewController.m // 03-contentOffset的使用 #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // UIImageView *imageView = [[UIImageView alloc] init]; // imageView.image = [UIImage imageNamed:@"minion"]; // imageView.frame = CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height); UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minion"]]; [self.scrollView addSubview:imageView]; // 设置内容大小 self.scrollView.contentSize = imageView.image.size; } - (IBAction)left:(id)sender { // self.scrollView.contentOffset : 偏移量 // 记录UIScrollView滚动的位置,滚到哪 // 总结:内容的左上角 和 scrollView自身左上角 的 X\Y差值 // [UIView animateWithDuration:2.0 animations:^{ // self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentOffset.y); // }]; //实现动画的方法1 [UIView animateWithDuration:2.0 animations:^{ self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentOffset.y); } completion:^(BOOL finished) { NSLog(@"执行完毕"); }]; } - (IBAction)top:(id)sender { // self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
//实现动画的方法2 CGPoint offset = CGPointMake(self.scrollView.contentOffset.x, 0); [self.scrollView setContentOffset:offset animated:YES]; } - (IBAction)bottom:(id)sender {
//实现动画的方法3 [UIView animateWithDuration:2.0 animations:^{ CGFloat offsetY = self.scrollView.contentSize.height - self.scrollView.frame.size.height; CGPoint offset = self.scrollView.contentOffset; offset.y = offsetY; self.scrollView.contentOffset = offset; }]; // self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, offsetY); // self.scrollView.contentOffset.y = offsetY; // OC语法细节:不允许直接修改OC对象的结构体属性的成员 } - (IBAction)right:(id)sender {
//实现动画的方法4 [UIView beginAnimations:nil context: nil]; [UIView setAnimationDuration:2.0]; [UIView setAnimationDelegate:self]; // 代理 [UIView setAnimationDidStopSelector:@selector(stop)]; [UIView setAnimationWillStartSelector:@selector(start)]; CGFloat offsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width; self.scrollView.contentOffset = CGPointMake(offsetX, self.scrollView.contentOffset.y); [UIView commitAnimations]; } - (void)start { NSLog(@"start"); } - (void)stop { NSLog(@"stop"); } @end
contentOffset的使用
// // ViewController.m // 03-contentOffset的使用 #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minion"]]; // [self.scrollView addSubview:imageView]; [self.scrollView insertSubview:imageView atIndex:0]; // 设置内容大小 self.scrollView.contentSize = imageView.image.size; // 设置contentInset self.scrollView.contentOffset = CGPointMake(0, -64); self.scrollView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0); } - (IBAction)left:(id)sender { self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentOffset.y); } - (IBAction)top:(id)sender { self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0); } - (IBAction)bottom:(id)sender { CGFloat offsetY = self.scrollView.contentSize.height - self.scrollView.frame.size.height; CGPoint offset = self.scrollView.contentOffset; offset.y = offsetY; self.scrollView.contentOffset = offset; } - (IBAction)right:(id)sender { CGFloat offsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width; self.scrollView.contentOffset = CGPointMake(offsetX, self.scrollView.contentOffset.y); } @end
原文:http://www.cnblogs.com/laugh/p/6398746.html