/** * 照片浏览的下标 1/8 * */
#import <UIKit/UIKit.h>
@interface PageLabel : UILabel
@property (nonatomic,assign)NSInteger numberOfPages;
@property (nonatomic,assign)NSInteger currentPage;
/** * page改变 */
- (void)labelText;
@end
#import <UIKit/UIKit.h>
@interface PageLabel : UILabel
@property (nonatomic,assign)NSInteger numberOfPages;
@property (nonatomic,assign)NSInteger currentPage;
/** * page改变 */
- (void)labelText;
@end
#import "PageLabel.h"
@implementation PageLabel
- (instancetype)init
{
self = [super init];
if (self) {
self.textColor = [UIColor whiteColor];
self.textAlignment = NSTextAlignmentCenter;
}
return self;
}
- (void)labelText {
if (_numberOfPages > 0) {
self.text = [NSString stringWithFormat:@"%ld/%ld",_currentPage+1,_numberOfPages];
}else {
self.text = nil;
}
}
@end
@implementation PageLabel
- (instancetype)init
{
self = [super init];
if (self) {
self.textColor = [UIColor whiteColor];
self.textAlignment = NSTextAlignmentCenter;
}
return self;
}
- (void)labelText {
if (_numberOfPages > 0) {
self.text = [NSString stringWithFormat:@"%ld/%ld",_currentPage+1,_numberOfPages];
}else {
self.text = nil;
}
}
@end
也可以用UIPageControl也差不多
图片浏览控制器
CycleScrollViewController.h
#import <UIKit/UIKit.h>
@protocol CycleScrollViewDelegate <NSObject>
- (void)updateui;
/**
* 删除
*
* @param array 返回改变后的数组
*/
- (void)cycleScrollViewDeletItem:(NSArray *)array;
@end
@interface CycleScrollViewController : UIViewController
@property (nonatomic,assign)id<CycleScrollViewDelegate>delegate;
/**
* 初始化
*
* @param mixId 数组(名字或者image)
* @param index 下标
* @param myself 是不是自己(如果是自己久传入image不是的话就传入网络地址)
*
* @return
*/
- (instancetype)initWithMixids:(NSArray *)mixId currentIndex:(int)index myself:(BOOL)myself;
@end
@protocol CycleScrollViewDelegate <NSObject>
- (void)updateui;
/**
* 删除
*
* @param array 返回改变后的数组
*/
- (void)cycleScrollViewDeletItem:(NSArray *)array;
@end
@interface CycleScrollViewController : UIViewController
@property (nonatomic,assign)id<CycleScrollViewDelegate>delegate;
/**
* 初始化
*
* @param mixId 数组(名字或者image)
* @param index 下标
* @param myself 是不是自己(如果是自己久传入image不是的话就传入网络地址)
*
* @return
*/
- (instancetype)initWithMixids:(NSArray *)mixId currentIndex:(int)index myself:(BOOL)myself;
@end
CycleScrollViewController.m
//
// ViewController.m
// test
//
// Created by 张鹏 on 14-4-30.
// Copyright (c) 2014年 rimi. All rights reserved.
//
#import "CycleScrollViewController.h"
#import "UIImageView+WebCache.h"
#import "SDImageCache.h"
#import "PageLabel.h"
@interface CycleScrollViewController () <UIScrollViewDelegate,UIAlertViewDelegate> {
NSMutableArray *_imageNames;
NSMutableArray *_imageViews;
UIScrollView *_scrollView;
UIPageControl *_pageControl;
NSInteger _currentPageIndex;
PageLabel *_labelText;
}
@property (nonatomic,strong) UITapGestureRecognizer *singleTap;
@property (nonatomic,strong) UITapGestureRecognizer *doubleTap;
@property (nonatomic,assign) CGFloat zoomScale;
@property (nonatomic,strong) UIDynamicAnimator *animator;
@property (nonatomic) BOOL myself;
@property (nonatomic) BOOL deleted;
- (void)initializeUserInterface;
//- (void)updateScrollViewWithContentOffset:(CGPoint)contentOffset;
//- (NSInteger)actualIndexWithIndex:(NSInteger)index;
@end
@implementation CycleScrollViewController
- (instancetype)initWithMixids:(NSArray *)mixId currentIndex:(int)index myself:(BOOL)myself{
self = [super init];
if (self) {
_myself=myself;
_deleted=NO;
_imageNames = [NSMutableArray arrayWithArray:mixId];
_imageViews = [NSMutableArray array];
_currentPageIndex = index;
}
return self;
}
- (void)dealloc {
_imageNames = nil;
_imageViews = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self initializeUserInterface];
}
- (void)initializeUserInterface {
self.view.backgroundColor = [UIColor blackColor];
self.automaticallyAdjustsScrollViewInsets = NO;
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectInset(self.view.bounds, -4, 0)];
_scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.bounds) * _imageNames.count,
CGRectGetHeight(_scrollView.bounds));
_scrollView.contentOffset = CGPointMake(CGRectGetWidth(_scrollView.bounds)*(_currentPageIndex), 0);
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
_scrollView.bouncesZoom = YES;
_scrollView.bounces = YES;
_scrollView.canCancelContentTouches = YES;
_scrollView.delaysContentTouches = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
if (_imageNames.count == 1) {
_scrollView.scrollEnabled = NO;
}
[self.view addSubview:_scrollView];
for (int i = 0; i < _imageNames.count; i++) {
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.bounds = self.view.frame;
scrollView.center = CGPointMake(CGRectGetWidth(_scrollView.bounds) / 2 + CGRectGetWidth(_scrollView.bounds) * i,
CGRectGetHeight(_scrollView.bounds) / 2);
scrollView.contentSize = self.view.frame.size;
scrollView.delegate = self;
scrollView.maximumZoomScale = 2.0;
scrollView.minimumZoomScale = 1.0;
scrollView.zoomScale = 1.0;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
[_scrollView addSubview:scrollView];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.layer.masksToBounds = YES;
imageView.userInteractionEnabled = YES;
[_imageViews addObject:imageView];
if (_imageNames) {
if (_myself) {
imageView.image = _imageNames[i];
}else {
[imageView sd_setImageWithURL:[NSURL URLWithString:_imageNames[i]]
placeholderImage:[UIImage imageNamed:@"four_contentimage"]
options:SDWebImageCacheMemoryOnly
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
}
}
[scrollView addSubview:imageView];
self.singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
self.singleTap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:self.singleTap];
self.doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
self.doubleTap.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:self.doubleTap];
[self.singleTap requireGestureRecognizerToFail:self.doubleTap];
}
_labelText = [[PageLabel alloc]init];
_labelText.bounds = CGRectMake(0, 0, 320, 20);
_labelText.center = CGPointMake(CGRectGetMidX(self.view.bounds),
CGRectGetMaxY(self.view.bounds) - 50);
if (_imageNames.count >1) {
_labelText.numberOfPages = [_imageNames count];
}
else {
_labelText.numberOfPages = 0;
}
[self.view addSubview:_labelText];
_labelText.currentPage = _currentPageIndex;
[_labelText labelText];
if (_myself) {
UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth-20)/2, 40, 20, 20)];
img.image=[UIImage imageNamed:@"ic_delete"];
UIButton *delbtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, 30)];
delbtn.backgroundColor=[UIColor clearColor];
[delbtn addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:img];
[self.view addSubview:delbtn];
}
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
}
- (void)popLastViewController {
[self.navigationController popViewControllerAnimated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)ButtonPressed{
UIAlertView *view=[[UIAlertView alloc] initWithTitle:nil message:@"确定要删除该照片?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[view show];
}
#pragma mark - tapGestureEvent
- (void)singleTap:(UITapGestureRecognizer *)tap {
if (_deleted) {
[_delegate updateui];
}
[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)doubleTap:(UITapGestureRecognizer *)tap {
UIScrollView *scrollView = (UIScrollView *)tap.view.superview;
NSLog(@"doubleTap");
// if (_isDetailShown) {
// return;
// }
if (scrollView.zoomScale == scrollView.minimumZoomScale) {
// Zoom in
CGPoint center = [tap locationInView:scrollView];
CGSize size = CGSizeMake(scrollView.bounds.size.width / scrollView.maximumZoomScale,
scrollView.bounds.size.height / scrollView.maximumZoomScale);
CGRect rect = CGRectMake(center.x - (size.width / 2.0), center.y - (size.height / 2.0), size.width, size.height);
[scrollView zoomToRect:rect animated:YES];
}
else {
// Zoom out
[scrollView zoomToRect:scrollView.bounds animated:YES];
}
}
#pragma mark - UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
for (UIView *v in scrollView.subviews){
return v;
}
return nil;
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int index= scrollView.contentOffset.x/self.view.bounds.size.width;
_currentPageIndex=index;
[_labelText setCurrentPage:index];
[_labelText labelText];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) {
[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
NSLog(@"%ld",_currentPageIndex);
for (int i = 0; i < _imageNames.count; i ++) {
if (i == _currentPageIndex) {
[_imageNames removeObjectAtIndex:i];
}
}
if ([_delegate respondsToSelector:@selector(cycleScrollViewDeletItem:)]) {
[_delegate cycleScrollViewDeletItem:_imageNames];
}
}
}
@end
// ViewController.m
// test
//
// Created by 张鹏 on 14-4-30.
// Copyright (c) 2014年 rimi. All rights reserved.
//
#import "CycleScrollViewController.h"
#import "UIImageView+WebCache.h"
#import "SDImageCache.h"
#import "PageLabel.h"
@interface CycleScrollViewController () <UIScrollViewDelegate,UIAlertViewDelegate> {
NSMutableArray *_imageNames;
NSMutableArray *_imageViews;
UIScrollView *_scrollView;
UIPageControl *_pageControl;
NSInteger _currentPageIndex;
PageLabel *_labelText;
}
@property (nonatomic,strong) UITapGestureRecognizer *singleTap;
@property (nonatomic,strong) UITapGestureRecognizer *doubleTap;
@property (nonatomic,assign) CGFloat zoomScale;
@property (nonatomic,strong) UIDynamicAnimator *animator;
@property (nonatomic) BOOL myself;
@property (nonatomic) BOOL deleted;
- (void)initializeUserInterface;
//- (void)updateScrollViewWithContentOffset:(CGPoint)contentOffset;
//- (NSInteger)actualIndexWithIndex:(NSInteger)index;
@end
@implementation CycleScrollViewController
- (instancetype)initWithMixids:(NSArray *)mixId currentIndex:(int)index myself:(BOOL)myself{
self = [super init];
if (self) {
_myself=myself;
_deleted=NO;
_imageNames = [NSMutableArray arrayWithArray:mixId];
_imageViews = [NSMutableArray array];
_currentPageIndex = index;
}
return self;
}
- (void)dealloc {
_imageNames = nil;
_imageViews = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self initializeUserInterface];
}
- (void)initializeUserInterface {
self.view.backgroundColor = [UIColor blackColor];
self.automaticallyAdjustsScrollViewInsets = NO;
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectInset(self.view.bounds, -4, 0)];
_scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.bounds) * _imageNames.count,
CGRectGetHeight(_scrollView.bounds));
_scrollView.contentOffset = CGPointMake(CGRectGetWidth(_scrollView.bounds)*(_currentPageIndex), 0);
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
_scrollView.bouncesZoom = YES;
_scrollView.bounces = YES;
_scrollView.canCancelContentTouches = YES;
_scrollView.delaysContentTouches = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
if (_imageNames.count == 1) {
_scrollView.scrollEnabled = NO;
}
[self.view addSubview:_scrollView];
for (int i = 0; i < _imageNames.count; i++) {
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.bounds = self.view.frame;
scrollView.center = CGPointMake(CGRectGetWidth(_scrollView.bounds) / 2 + CGRectGetWidth(_scrollView.bounds) * i,
CGRectGetHeight(_scrollView.bounds) / 2);
scrollView.contentSize = self.view.frame.size;
scrollView.delegate = self;
scrollView.maximumZoomScale = 2.0;
scrollView.minimumZoomScale = 1.0;
scrollView.zoomScale = 1.0;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
[_scrollView addSubview:scrollView];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.layer.masksToBounds = YES;
imageView.userInteractionEnabled = YES;
[_imageViews addObject:imageView];
if (_imageNames) {
if (_myself) {
imageView.image = _imageNames[i];
}else {
[imageView sd_setImageWithURL:[NSURL URLWithString:_imageNames[i]]
placeholderImage:[UIImage imageNamed:@"four_contentimage"]
options:SDWebImageCacheMemoryOnly
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
}
}
[scrollView addSubview:imageView];
self.singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
self.singleTap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:self.singleTap];
self.doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
self.doubleTap.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:self.doubleTap];
[self.singleTap requireGestureRecognizerToFail:self.doubleTap];
}
_labelText = [[PageLabel alloc]init];
_labelText.bounds = CGRectMake(0, 0, 320, 20);
_labelText.center = CGPointMake(CGRectGetMidX(self.view.bounds),
CGRectGetMaxY(self.view.bounds) - 50);
if (_imageNames.count >1) {
_labelText.numberOfPages = [_imageNames count];
}
else {
_labelText.numberOfPages = 0;
}
[self.view addSubview:_labelText];
_labelText.currentPage = _currentPageIndex;
[_labelText labelText];
if (_myself) {
UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth-20)/2, 40, 20, 20)];
img.image=[UIImage imageNamed:@"ic_delete"];
UIButton *delbtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, 30)];
delbtn.backgroundColor=[UIColor clearColor];
[delbtn addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:img];
[self.view addSubview:delbtn];
}
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
}
- (void)popLastViewController {
[self.navigationController popViewControllerAnimated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)ButtonPressed{
UIAlertView *view=[[UIAlertView alloc] initWithTitle:nil message:@"确定要删除该照片?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[view show];
}
#pragma mark - tapGestureEvent
- (void)singleTap:(UITapGestureRecognizer *)tap {
if (_deleted) {
[_delegate updateui];
}
[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)doubleTap:(UITapGestureRecognizer *)tap {
UIScrollView *scrollView = (UIScrollView *)tap.view.superview;
NSLog(@"doubleTap");
// if (_isDetailShown) {
// return;
// }
if (scrollView.zoomScale == scrollView.minimumZoomScale) {
// Zoom in
CGPoint center = [tap locationInView:scrollView];
CGSize size = CGSizeMake(scrollView.bounds.size.width / scrollView.maximumZoomScale,
scrollView.bounds.size.height / scrollView.maximumZoomScale);
CGRect rect = CGRectMake(center.x - (size.width / 2.0), center.y - (size.height / 2.0), size.width, size.height);
[scrollView zoomToRect:rect animated:YES];
}
else {
// Zoom out
[scrollView zoomToRect:scrollView.bounds animated:YES];
}
}
#pragma mark - UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
for (UIView *v in scrollView.subviews){
return v;
}
return nil;
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int index= scrollView.contentOffset.x/self.view.bounds.size.width;
_currentPageIndex=index;
[_labelText setCurrentPage:index];
[_labelText labelText];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) {
[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
NSLog(@"%ld",_currentPageIndex);
for (int i = 0; i < _imageNames.count; i ++) {
if (i == _currentPageIndex) {
[_imageNames removeObjectAtIndex:i];
}
}
if ([_delegate respondsToSelector:@selector(cycleScrollViewDeletItem:)]) {
[_delegate cycleScrollViewDeletItem:_imageNames];
}
}
}
@end