block代替协议自定义UIScrollView实现分页效果
2014-08-20 21:35
459 查看
#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
@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
相关文章推荐
- 安卓中自定义view控件代替radiogroup实现颜色渐变效果的写法
- UIScrollView实现自定义分页的大小
- [iOS]UIScrollview自定义分页的实现方法
- UIScrollView的分页效果的实现
- IOS基础UI之(九) UIPageControl结合UIScrollView实现分页效果
- 可自定义分页宽度的UIScrollView(Swift实现)
- UIScrollView实现分页效果
- iPhone 使用UIScrollView实现Spring滚屏效果
- UIScrollView分页的实现
- 自定义TextView实现跑马灯效果
- iOS 6编程-UIScrollView滚动视图和UIPageControl分页控件实现图像分页显示
- iOS 6编程-UIScrollView滚动视图和UIPageControl分页控件实现图像分页显示(2)
- Android自定义View实现HTML图文环绕效果
- 自定义TextView实现跑马灯效果
- android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
- 使用自定义RadioButton和ViewPager实现TabHost效果和带滑动的页卡效果。
- 以uiscrollview实现的图片放大效果
- UIScrollView分页的实现
- iOS UIScrollView中实现分页滚动--定时自动滚动分页
- 自定义ViewGroup 实现拖动跟快速滚动的效果