DCIM 用 UIScrollView 和UIPageControl
2015-08-12 22:40
573 查看
准备工作不赘述
相册 点开之后点击每个图片跳转到图片的详情面,滑动图片下面的UIPageControl跟着滑动
MainViewController.m
SecondViewController.h
SecondViewController.m
创建一个LTView 在LTView中创建一个button,注意button的type为custom
LTView.h
LTView.m
相册 点开之后点击每个图片跳转到图片的详情面,滑动图片下面的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
相关文章推荐
- js实现EasyUI-datagrid前台分页
- Leetcode63 Unique Paths II
- UIPageControl
- UI 06 _ 7种手势
- 消息队列(message queue)产生的背景、常用的技术
- hdu 1297 Children’s Queue(递推和高精)
- IOS--UI--NSThread 和GCD 总结
- Design Pattern -- Builder
- UI05 _ 分段控件 -- UISegmentedControl
- UIImageView+GestureRecognizer
- prim Building a Space Station
- iOS开发-UIScreenEdgePanGestureRecognizer实战
- UI 05 _ GIF 图的播放
- 优化UITableViewCell高度计算的那些事
- 读书笔记-APUE第三版-(11)线程
- UIButton在Disabled状态下标题混乱的问题
- UI11_快捷菜单
- 深入了解UIAutomation 的API
- UI Block传值
- UITabelBar