示例代码
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView* sv = [[UIScrollView alloc] init];
sv.frame = CGRectMake(0, 0, 320, 576);
sv.pagingEnabled = YES;
sv.scrollEnabled = YES;
sv.contentSize = CGSizeMake(320*5, 576);
sv.bounces = YES;
sv.alwaysBounceHorizontal = YES;
sv.alwaysBounceVertical = YES;
sv.showsHorizontalScrollIndicator = YES;
sv.showsVerticalScrollIndicator = YES;
sv.backgroundColor = [UIColor yellowColor];
for (int i = 0; i < 2; i++) {
NSString* strName = [NSString stringWithFormat:@"%d.jpg", i+1];
UIImage* image = [UIImage imageNamed:strName];
UIImageView* iView = [[UIImageView alloc] initWithImage:image];
iView.frame = CGRectMake(320*i, 0, 320, 576);
[sv addSubview:iView];
}
[self.view addSubview:sv];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIScrollViewDelegate>
{
UIScrollView* _scrollView;
}
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_scrollView = [[UIScrollView alloc] init];
_scrollView.frame = CGRectMake(10, 50, 300, 400);
_scrollView.bounces = NO;
_scrollView.contentSize = CGSizeMake(300, 400*2);
for (int i = 0; i < 2; i++) {
NSString* strName = [NSString stringWithFormat:@"%d.jpg", i+1];
UIImage* image = [UIImage imageNamed:strName];
UIImageView* iView = [[UIImageView alloc] init];
iView.image = image;
iView.frame = CGRectMake(0, 400*i, 300, 400);
[_scrollView addSubview:iView];
}
[self.view addSubview:_scrollView];
_scrollView.pagingEnabled = YES;
_scrollView.contentOffset = CGPointMake(0, 0);
_scrollView.delegate = self;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
_scrollView.contentOffset = CGPointMake(0, 0);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"Did End Drag!");
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"Will Begin Drag");
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
NSLog(@"Will End Drag!");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
NSLog(@"Will Begin Deceleratg");
}
- (void)didReceiveMemoryWarning
{
NSLog(@"ting");
}
@end
心得体会
- 应该说滚屏就好像设置了屏幕大小,而画布想象成连续的一块布垫在滚屏下面(或者说滚屏是一个框,内容已经放在了画布上,通过移动框看见不同的图片)
- 一旦取消触碰事件就等于是变成静止图片,难以对其操作