#import <UIKit/UIKit.h>
@interface CustomScrollView : UIView
//頁數
@property (nonatomic, strong) NSInteger (^numberOfPageInScrollView)(CustomScrollView*scrollView);
//返回一個View,這裏顯示圖片,當前頁面
@property (nonatomic, strong) UIView *(^scroll)(CustomScrollView *scrollView,NSInteger page);
//點擊取得當前頁面
@property (nonatomic, strong) void (^didSelectAtPage)(CustomScrollView *scrollView,NSInteger page);
//當前頁
@property (nonatomic, assign) NSInteger currentPage;
//加載頁數,圖片等數據
- (void)reloadData;
@end
#import "CustomScrollView.h"
@interface CustomScrollView () <UIScrollViewDelegate>
{
UIScrollView *_scrollView;
NSInteger _currentPage;
}
@end
@implementation CustomScrollView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.pagingEnabled = YES;//設置能翻頁
_scrollView.delegate = self;
_scrollView.showsHorizontalScrollIndicator = NO;//鎖定只能水平滑動
[self addSubview:_scrollView];
//增加一個點擊手勢,用來獲得點擊view得到響應
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
[self addGestureRecognizer:tap];
}
return self;
}
//加載數據
- (void)reloadData
{
//得到頁數
NSInteger count = _numberOfPageInScrollView(self);
CGFloat width = self.frame.size.width;//寬度
CGFloat height = self.frame.size.height;//高度
_scrollView.contentSize = CGSizeMake(width * count, height);//設置內容寬度和高度
//生成對應頁數的view
for (NSInteger i = 0; i < count; i++) {
UIView *view = _scroll(self,i);
view.frame = CGRectMake(i * width, 0, width, height);
[_scrollView addSubview:view];
}
}
- (void)didTap:(NSInteger)page
{
NSInteger index = _scrollView.contentOffset.x / _scrollView.frame.size.width;
_didSelectAtPage(self,index);
//提示窗口
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"當前頁"
message:[NSString stringWithFormat:@"%d",index]
delegate:self
cancelButtonTitle:@"cance"
otherButtonTitles: nil];
[alert show];
}
- (NSInteger)currentPage
{
return _scrollView.contentOffset.x / _scrollView.frame.size.width;
}
- (void)setCurrentPage:(NSInteger)currentPage
{
_scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width * currentPage, 0);
_currentPage = currentPage;
}
//滑動後鬆開手指速度爲0時執行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSInteger index = _scrollView.contentOffset.x / _scrollView.frame.size.width;
//點擊的是那一頁
_didSelectAtPage(self,index);
}
@end
#import "ViewController.h"
#import "CustomScrollView.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CustomScrollView *scrollView = [[CustomScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
scrollView.currentPage = 9;
[self.view addSubview:scrollView];
//設置滾動頁面
scrollView.numberOfPageInScrollView = ^(CustomScrollView *add){
return 10;
};
scrollView.scroll = ^(CustomScrollView *as,NSInteger page){
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"1.jpg"];
UILabel*label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 100, 30)];
label1.text = [NSString stringWithFormat:@"Page :%d",page];
if (page%2) {
label1.backgroundColor = [UIColor magentaColor];
}
//在block裏不能用scrollView來調用
//變量名字不好取
as.didSelectAtPage = ^(CustomScrollView *as,NSInteger inte)
{
NSLog(@"%d",page);
};
[imageView addSubview:label1];
return imageView;
};
//怎麼自動調用是個問題
[scrollView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end