博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图片照片类的封装(不是相册)
阅读量:5908 次
发布时间:2019-06-19

本文共 8398 字,大约阅读时间需要 27 分钟。

/*
* *  照片浏览的下标 1/8 * 
*/
#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

 也可以用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

 

 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

 

转载于:https://www.cnblogs.com/hxwj/p/4594739.html

你可能感兴趣的文章
JQuery实战--可以编辑的表格
查看>>
公有云行业:用价格撕开市场,用质量取胜行业
查看>>
千兆宽带,谁买账
查看>>
如何在三年内快速成长为一名技术专家
查看>>
java项目实战——Java打飞机小游戏(附完整源码)
查看>>
ASP.NET的路由系统:URL与物理文件的分离
查看>>
轻松入门Android直播相关技术 从0搭建直播系统
查看>>
以css为例谈设计模式
查看>>
Protecting Websites through Semantics-Based Malware Detection
查看>>
JavaScript:到底什么是ES6、ES8、ES 2017、ECMAScript?
查看>>
计算机网络总结之计算机概述
查看>>
YOCSEF:集众家之言诠释云计算缘起
查看>>
MONGOOSE简要API
查看>>
WiFi万能钥匙首席安全官:公共WiFi风险占比仅0.81%
查看>>
2015年云计算或将成为企业主流应用
查看>>
“暗黑流量”超大规模DDoS溯源分析
查看>>
中国人工智能学会通讯——基于众包的数据清洗模型研究 1 问题定义
查看>>
数字化和网络化双重驱动下 周界安防产品迎来新变革
查看>>
工程造价行业正处大数据时代
查看>>
EMC发布第四季度财报 实现预期盈利目标
查看>>