DCIM 用 UIScrollView 和UIPageControl

准备工作不赘述

相册 点开之后点击每个图片跳转到图片的详情面,滑动图片下面的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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值