scrollView-contentOffset和UIViewAnimation动画执行

ScrollView的基本概念

ScrollView是什么?

  • 移动设备的屏幕大小是极其有限的,因此直接展示在用户眼前的内容也相当有限
  • 当展示的内容较多,超出一个屏幕时,用户可通过滚动手势来查看屏幕以外的内容
  • 普通的UIView不具备滚动功能,不适合显示过多的内容
  • UIScrollView是一个能够滚动的视图控件,可以用来展示大量的内容,并且可以通过滚动查看所有的内容
    举例:手机上的“设置”

ScrollView的基本使用

  • 将需要展示的内容添加到UIScrollView中
  • 设置UIScrollView的contentSize属性,里面添加的内容就可以滚动了,(ScrollView不动,frame作为显示区域,里面的内容移动)
  • 超出UIScrollView边框的内容会被自动隐藏

contentOffSet举例使用


//@property(nonatomic) CGPoint contentOffset;
//这个属性用来表示UIScrollView滚动的位置
//偏移量(其实就是内容左上角与scrollView左上角X和Y的间距值)

#import "WQViewController.h"

@interface WQViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
/**图片*/
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation WQViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // 用此方法设置图片,默认image和imageView的frame相同
    _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"minion"]];
    [self.scrollView addSubview:_imageView];
    // 设置滚动区域,ScrollView中添加的内容就可以移动,
    // contentSize为滚动范围,内容可显示到ScrollView上的区域,以左上角为原点.
    self.scrollView.contentSize = CGSizeMake(_imageView.frame.size.width , _imageView.frame.size.height);
}
#pragma 移动过程中增加动画
/** 上侧*/
- (IBAction)topBtn:(id)sender {
    // 移动过程有动画效果
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2];
    self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
    [UIView commitAnimations];

}
#pragma 通过代理监听动画执行过程
/** 下方*/
- (IBAction)bottomBtn:(id)sender {
    // 如果想监听动画执行过程,做一些事情,必须要实现动画代理
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2];
    // 控制器作为动画代理
    [UIView setAnimationDelegate:self];
    // 动画执行完会调用代理的自定义方法
    [UIView setAnimationDidStopSelector:@selector(stop)];
    [UIView setAnimationWillStartSelector:@selector(start)];
    // 两秒后执行动画
    [UIView setAnimationDelay:2];
    [UIView setAnimationRepeatCount:2.0];
    [UIView setAnimationRepeatAutoreverses:YES];
    self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, self.imageView.frame.size.height - self.scrollView.frame.size.height);
    [UIView commitAnimations];

    /**
     // 设置动画时间
     + (void)setAnimationDuration:(NSTimeInterval)duration;              // default = 0.2
     // 设置动画多久后执行
     + (void)setAnimationDelay:(NSTimeInterval)delay;                    // default = 0.0
     + (void)setAnimationStartDate:(NSDate *)startDate;                  // default = now ([NSDate date])
     // 执行
     + (void)setAnimationCurve:(UIViewAnimationCurve)curve;              // default = UIViewAnimationCurveEaseInOut
     // 动画执行次数
     + (void)setAnimationRepeatCount:(float)repeatCount;                 // default = 0.0.  May be fractional
     // 每重复执行一次,先回到原来状态,再执行下一次.最后状态和自动回复无关
     + (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;    // default = NO. used if repeat count is non-zero
     + (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState;


     typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
     // 先慢后快最后慢
     UIViewAnimationCurveEaseInOut,         // slow at beginning and end
     // 开始变快后变慢
     UIViewAnimationCurveEaseIn,            // slow at beginning
     // 先快最后变慢
     UIViewAnimationCurveEaseOut,           // slow at end
     // 线性变化
     UIViewAnimationCurveLinear
     };
     */
}
- (void)start
{
    NSLog(@"动画将要执行,会调用代理中的定义的此方法");
}
- (void)stop
{
    NSLog(@"动画执行完,会调用代理中的定义的此方法");
}
#pragma mark 通过block执行动画
/** 左侧*/
- (IBAction)leftBtn:(id)sender {
    [UIView animateWithDuration:2 animations:^{
        self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentOffset.y);

    }];
}
/** 右侧*/
- (IBAction)rightBtn:(id)sender {
    [UIView animateWithDuration:2 animations:^{
        self.scrollView.contentOffset = CGPointMake(self.imageView.frame.size.width - self.scrollView.frame.size.width, self.scrollView.contentOffset.y);
    } completion:^(BOOL finished) {
        NSLog(@"动画执行完毕");
    }];

}
/** 右上*/
- (IBAction)rightTopBtn:(id)sender {
    [UIView animateWithDuration:2 delay:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.scrollView.contentOffset = CGPointMake(self.imageView.frame.size.width - self.scrollView.frame.size.width, 0);
    } completion:^(BOOL finished) {
        NSLog(@"动画完成");
    }];

    /*
     [UIView animateWithDuration:<#(NSTimeInterval)#> delay:<#(NSTimeInterval)#> usingSpringWithDamping:<#(CGFloat)#> initialSpringVelocity:<#(CGFloat)#> options:<#(UIViewAnimationOptions)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>]
     */

}
/** 左下*/
- (IBAction)leftBottomBtn:(id)sender {

    self.scrollView.contentOffset = CGPointMake(0, self.imageView.frame.size.height - self.scrollView.frame.size.height);
}
/** 右下*/
- (IBAction)rightBottomBtn:(id)sender {
    self.scrollView.contentOffset = CGPointMake(self.imageView.frame.size.width - self.scrollView.frame.size.width, self.imageView.frame.size.height - self.scrollView.frame.size.height);
}

#pragma mark contentOffset特有的动画
/** 左上*/
- (IBAction)leftTopBtn:(id)sender {
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
@end

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值