[IOS笔记]UIScrollView属性

来源:互联网 发布:数据库设计原则,范式 编辑:程序博客网 时间:2024/06/06 18:42




////  ViewController.m//  UIScrollView展示大图片////  Created by cdj on 17/9/10.//  Copyright © 2017年 ue. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];/*    UIImage *image = [UIImage imageNamed:@"minion"];    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];        [self.scrollView addSubview:imageView];    //    设置contentSize//    self.scrollView.contentSize = CGSizeMake(510, 510);//    self.scrollView.contentSize = imageView.frame.size;    self.scrollView.contentSize = image.size;    //    去掉弹簧效果//    self.scrollView.bounces = NO;//    self.scrollView.alwaysBounceHorizontal = NO;//    self.scrollView.alwaysBounceVertical = NO;    //    默认显示滚动条    self.scrollView.showsHorizontalScrollIndicator = NO;    self.scrollView.showsVerticalScrollIndicator = YES;        NSLog(@"%@", self.scrollView.subviews);    //    注意:不要通过索引去subviews数组访问scrollView子控件//    [self.scrollView.subviews.firstObject removeFromSuperview]; */    /*//    下拉刷新的菊花    UIActivityIndicatorView *indicatorView =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];        indicatorView.center = CGPointMake(100, -40);    [indicatorView startAnimating];    [self.scrollView addSubview:indicatorView];        self.scrollView.alwaysBounceHorizontal = YES;    self.scrollView.alwaysBounceVertical = YES; */        UIImage *image = [UIImage imageNamed:@"minion"];    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];        [self.scrollView addSubview:imageView];    self.scrollView.contentSize = image.size;    //    内容的偏移量,作用:控制内容滚动的位置;得知内容滚动的位置    self.scrollView.contentOffset = CGPointMake(100, 100);        //    设置剩余的内边距:作用    self.scrollView.contentInset = UIEdgeInsetsMake(10, 20, 30, 40);                    }/** *  Description点击控制器的view会自动调用这个方法 * *  @param touches <#touches description#> *  @param event   <#event description#> */-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    NSLog(@"touchesBegan");    self.scrollView.contentOffset = CGPointMake(-100, -100);}- (IBAction)top {    /*//     方式一    CGPoint offset = self.scrollView.contentOffset;    offset.y = 0;    self.scrollView.contentOffset = offset;     */    //    方式二//    self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);    //    方式三:有动画,但是无法设定时间//    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x, 0) animated:YES];    //    方式四    [UIView animateWithDuration:2.0 animations:^{        self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);        }];}- (IBAction)bottom {    CGFloat offsetX = self.scrollView.contentOffset.x;    CGFloat offsetY = self.scrollView.contentSize.height - self.scrollView.frame.size.height;    CGPoint offset = CGPointMake(offsetX, offsetY);    [self.scrollView setContentOffset:offset animated:YES];}- (IBAction)left {    [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y) animated:YES];}- (IBAction)right {    CGFloat offsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width;    CGFloat offsetY = self.scrollView.contentOffset.y;    CGPoint offset = CGPointMake(offsetX, offsetY);    [self.scrollView setContentOffset:offset animated:YES];    }- (IBAction)rightTop {    CGFloat offsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width;    CGFloat offsetY = 0;    CGPoint offset = CGPointMake(offsetX, offsetY);    [self.scrollView setContentOffset:offset animated:YES];}- (IBAction)leftBottom {    CGFloat offsetX = 0;    CGFloat offsetY = self.scrollView.contentSize.height - self.scrollView.frame.size.height;    CGPoint offset = CGPointMake(offsetX, offsetY);    [self.scrollView setContentOffset:offset animated:YES];}@end




















































原创粉丝点击