首页 > 其他 > 详细

源码-0103-scrollview

时间:2017-02-14 18:56:43      阅读:124      评论:0      收藏:0      [点我收藏+]

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

 

源码-0103-scrollview

原文:http://www.cnblogs.com/laugh/p/6398746.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!