准备工作不赘述
相册 点开之后点击每个图片跳转到图片的详情面,滑动图片下面的UIPageControl跟着滑动
MainViewController.m
#import "MainViewController.h"
#import "LTView.h"
#import "SecondViewController.h"
@interface MainViewController ()
@property(nonatomic,retain)NSArray *arr;
@end
@implementation MainViewController
-(void)dealloc
{
[_arr release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.translucent=NO;
self.title=@"DCIM";
self.arr=[NSArray alloc];
for (NSInteger i=0; i<3; i++) {
for (NSInteger j=0; j<3; j++) {
LTView *view=[[LTView alloc]initWithFrame:CGRectMake(30+105*j, 30+135*i, 100, 130)];
[view.button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"h%ld.jpeg",j+1+3*i]] forState:UIControlStateNormal];
[view.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:view];
[view release];
view.button.tag = j + 1 + 3 * i;
}
}
}
-(void)click:(UIButton *)button
{
SecondViewController *secVC=[[SecondViewController alloc]init];
[self.navigationController pushViewController:secVC animated:YES];
secVC.num = button.tag;
[secVC release];
}
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property(nonatomic,assign)NSInteger num;
@end
SecondViewController.m
#import "SecondViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height - 64
@interface SecondViewController ()<UIScrollViewDelegate>
@property(nonatomic,retain)UIScrollView *scrollView;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = [NSString stringWithFormat:@"第%ld页",self.num ];
self.scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0 , 0, WIDTH, HEIGHT)];
[self.view addSubview:self.scrollView];
[self.scrollView release];
self.scrollView.contentSize=CGSizeMake(WIDTH * 9,HEIGHT );
self.scrollView.pagingEnabled=YES;
for (NSInteger i = 0; i<9; i++) {
UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"h%ld.jpeg",i+1]]];
imageView.frame=CGRectMake(WIDTH * i , 0, WIDTH, HEIGHT);
[self.scrollView addSubview:imageView];
[imageView release];
}
self.scrollView.contentOffset = CGPointMake(WIDTH *(self.num-1), 0);
self.scrollView.delegate=self;
UIPageControl *pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(90, 540, 200, 20)];
[self.view addSubview:pageControl];
pageControl.numberOfPages=9;
pageControl.pageIndicatorTintColor=[UIColor whiteColor];
pageControl.currentPageIndicatorTintColor=[UIColor redColor];
pageControl.tag=1;
[pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
}
-(void)pageAction:(UIPageControl *)pageControl
{
[self.scrollView setContentOffset:CGPointMake(pageControl.currentPage *WIDTH, 0) animated:YES];
self.title=[NSString stringWithFormat:@"第%ld页",pageControl.currentPage + 1];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
UIPageControl *pageControl=(UIPageControl *)[self.view viewWithTag:1];
pageControl.currentPage=self.scrollView.contentOffset.x/WIDTH;
self.title=[NSString stringWithFormat:@"%g页",self.scrollView.contentOffset.x/WIDTH + 1];
}
创建一个LTView 在LTView中创建一个button,注意button的type为custom
LTView.h
#import <UIKit/UIKit.h>
@interface LTView : UIView
@property(nonatomic,retain)UIButton *button;
@end
LTView.m
#import "LTView.h"
@implementation LTView
-(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];// 这个事情UIView还会做,LTView继承与UIView
if (self) {
// 在代码区写额外的事就可以了
// 模块化
// 自己调用自己的方法
[self createButton];
}
return self;
}
-(void)createButton
{
self.button=[UIButton buttonWithType:UIButtonTypeCustom];
self.button.frame=CGRectMake(0, 0, 100, 130);
self.button.backgroundColor=[UIColor redColor];
// self.button.layer.borderWidth=1;
[self addSubview:self.button];
}
@end