- 移动设备的屏幕大小是极其有限的,因此直接展示在用户眼前的内容也相当有限
- 当展示的内容较多,超出一个屏幕时,用户可通过滚动手势来查看屏幕以外的内容
- 普通的UIView不具备滚动功能,不适合显示过多的内容
- UIScrollView是一个能够滚动的视图控件,可以用来展示大量的内容,并且可以通过滚动查看所有的内容
举例:手机上的“设置”
- 将需要展示的内容添加到UIScrollView中
- 设置UIScrollView的contentSize属性,里面添加的内容就可以滚动了,(ScrollView不动,frame作为显示区域,里面的内容移动)
- 超出UIScrollView边框的内容会被自动隐藏
contentOffSet举例使用
#import "WQViewController.h"
@interface WQViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
/**图片*/
@property (nonatomic, strong) UIImageView *imageView;
@end
@implementation WQViewController
- (void)viewDidLoad {
[super viewDidLoad];
_imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"minion"]];
[self.scrollView addSubview:_imageView];
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(@"动画完成");
}];
}
/** 左下*/
- (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
