#import <UIKit/UIKit.h>
@interface DetailView : UIView <UIScrollViewDelegate>
@property(nonatomic,retain)UIView *upView;
@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic,retain)UIPageControl *pageControl;
@end
#import "DetailView.h"
@implementation DetailView
@synthesize upView;
@synthesize scrollView;
@synthesize pageControl;
const CGFloat kScrollHeight = 320; //图片的高度与宽度
const CGFloat kScrollWidth = 300;
const CGFloat imagePreView = 3; //每页显示图片数量
const NSInteger numberOfImage = 4; //总展示图片数
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self buildView];
}
return self;
}
- (void)buildView
{
self.backgroundColor = [UIColor orangeColor];
//上部工具条
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[backBtn setFrame:CGRectMake(20, 10, 40, 25)];
[closeBtn setBackgroundImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
[closeBtn setFrame:CGRectMake(880, 10, 25, 25)];
[backBtn addTarget:self action:@selector(backBtnPressed) forControlEvents:UIControlEventTouchUpInside];
[closeBtn addTarget:self action:@selector(backBtnPressed) forControlEvents:UIControlEventTouchUpInside];
upView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 920, 40)];
upView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"upvie.png"]];
[upView addSubview:backBtn];
[upView addSubview:closeBtn];
[self addSubview:upView];
//scrollView 滑动
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(15, 50, kScrollWidth*imagePreView-10, kScrollHeight)];
scrollView.delegate = self;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.backgroundColor = [UIColor purpleColor];
[scrollView setPagingEnabled:YES];
[self scrollViewCycle];
[self addSubview:scrollView];
//翻页 pageControll
pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(410, 450, 100, 50)];
pageControl.userInteractionEnabled = YES;
pageControl.numberOfPages = 4;
pageControl.currentPage = 0;
[pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
[self addSubview:pageControl];
pageControl.currentPage = 0;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int index = fabs(self.scrollView.contentOffset.x)/self.scrollView.frame.size.width;
pageControl.currentPage = index;
//int index = fabs(self.scrollView.contentOffset.x)/self.scrollView.frame.size.width;
}
- (void)scrollViewCycle
{
for (int i = 1; i <= imagePreView*4; i++) {
int d = i%4 +1;
NSString *imageName = [NSString stringWithFormat:@"%d.jpg",d];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = CGRectMake(0, 0, kScrollWidth-10, kScrollHeight);
imageView.tag = i;
[scrollView addSubview:imageView];
}
UIImageView *imageV = nil;
NSArray *subviews = [scrollView subviews];
CGFloat curXLoc = 0;
for(imageV in subviews){
if ([imageV isKindOfClass:[UIImageView class]] && imageV.tag > 0 ) {
CGRect frame = imageV.frame;
frame.origin = CGPointMake(curXLoc, 0);
imageV.frame = frame;
curXLoc += kScrollWidth;
}
[scrollView setContentSize:CGSizeMake((imagePreView*4* kScrollWidth), [scrollView bounds].size.height)];
}
}
- (void)changePage:(id)sender {
int page = pageControl.currentPage;
[scrollView setContentOffset:CGPointMake(kScrollWidth * page *3, 0)];
}
- (void)backBtnPressed
{
self.hidden = YES;
}
@end