开发思想 自定义UIView ,在UIView中添加scrollView,
使用场景 新闻首页滚动效果 so on
//
// BookViewController.m@end
//
// ADView.m
#import "ADView.h"
#import "MyControl.h"
@interface ADView() <UIScrollViewDelegate>
@end
@implementation ADView
{
UIScrollView *_scrollView;
UIPageControl *_pageControl;
UILabel *_label;
}
- (void)dealloc{
[_scrollView release];
[_pageControl release];
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self showUI];
}
return self;
}
- (void)showUI{
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 160)];
for (int i = 0; i < 4; i++) {
UIImageView *image = [MyControl creatImageViewWithFrame:CGRectMake(320*i, 0, 320, 160) image:[NSString stringWithFormat:@"image%d",i]];
[_scrollView addSubview:image];
}
_scrollView.contentSize = CGSizeMake(320*4, 160);
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
[self addSubview:_scrollView];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 130, 320, 30)];
view.backgroundColor = [UIColor blackColor];
view.alpha = 0.5;
_label = [MyControl creatLabelWithFrame:CGRectMake(10, 0, 200, 30) text:@"图片0的标题"];
_label.textColor = [UIColor whiteColor];
[view addSubview:_label];
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(200, 0, 120, 30)];
_pageControl.numberOfPages = 4;
//添加翻页触发事件
[_pageControl addTarget:self action:@selector(pageControl:) forControlEvents:UIControlEventValueChanged];
[view addSubview:_pageControl];
[self addSubview:view];
[view release];
}
//控制滚动
- (void)pageControl:(UIPageControl *)page{
[_scrollView setContentOffset:CGPointMake(_pageControl.currentPage*320, 0) animated:YES];
_label.text = [NSString stringWithFormat:@"图片%ld的标题",page.currentPage];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
CGPoint point = scrollView.contentOffset;
_pageControl.currentPage = point.x/320;
_label.text = [NSString stringWithFormat:@"图片%ld的标题",_pageControl.currentPage];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"ScrollView gun ");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end