文档章节

制作相册

langzhouzhou1
 langzhouzhou1
发布于 2016/12/01 09:05
字数 1468
阅读 20
收藏 0

主要利用了scrollview的viewForZoomingInScrollView以及scrollViewDidEndZooming实现放大缩小。

先写一个基类

#import <UIKit/UIKit.h>

@protocol XJScrollViewProtpcol <NSObject>

- (void)pageNum:(NSInteger)num;

@end

@interface XJScrollView : UIScrollView

@property (nonatomic, assign)NSInteger pageNum;                         //一共页数
@property (nonatomic, weak)id<XJScrollViewProtpcol> pageNumDelegate;
@property (nonatomic, assign)NSInteger showPageNum;                     //显示某一页(无动画)
@property (nonatomic, assign)NSInteger showPageNumNoAnimated;           //显示某一页(动画)

@end

#import "XJScrollView.h"

@interface XJScrollView ()<UIScrollViewDelegate>
{

}

@end
@implementation XJScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createContent];
    }
    return self;
}

- (void)createContent
{
    self.pagingEnabled = YES;
    self.delegate = self;
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
}

- (void)setShowPageNum:(NSInteger)showPageNum
{
    _showPageNum = showPageNum;
    
    CGPoint page = CGPointMake(showPageNum*self.frame.size.width, 0);
    [self setContentOffset:page animated:YES];
}

- (void)setShowPageNumNoAnimated:(NSInteger)showPageNumNoAnimated
{
    _showPageNumNoAnimated = showPageNumNoAnimated;
    CGPoint page = CGPointMake(showPageNumNoAnimated*self.frame.size.width, 0);
    [self setContentOffset:page animated:NO];
    
}
- (void)setPageNum:(NSInteger)pageNum
{
    if (_pageNum != pageNum) {
        _pageNum = pageNum;
        
        self.contentSize = CGSizeMake(self.frame.size.width * _pageNum, self.frame.size.height);
        
    }
}
#pragma mark - scrollView Deldgate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender
{
    CGFloat pageWidth = sender.frame.size.width;
    int currentPage = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
    if (_pageNumDelegate && [_pageNumDelegate respondsToSelector:@selector(pageNum:)]) {
        [_pageNumDelegate pageNum:currentPage];
    }
}

- (void) scrollViewDidScroll:(UIScrollView *)sender {
    
}
@end

然后需要在一个scrollview上面在加多个scrollview达到一个相册效果,先从一张照片开始

PhotoView

#import "XJScrollView.h"
typedef void(^LoadPercentage)(double);
typedef void(^LoadFinish)(BOOL, NSError*);

@protocol PhotoViewDelegate <NSObject>

@optional
- (void)oneFingerOneClick:(NSInteger)tag;

@end

@interface PhotoView : XJScrollView

@property (nonatomic, strong)UIImageView * photo;
@property (nonatomic, strong)LoadPercentage percentBlock;
@property (nonatomic, strong)LoadFinish loadFinish;
@property (nonatomic, copy)NSString * imageURL;
@property (nonatomic, copy)NSString * locaStr;
@property (nonatomic, weak)id<PhotoViewDelegate> photoDelegate;

@end

#import "PhotoView.h"
#import "PhototDefinde.h"
@interface PhotoView()<UIGestureRecognizerDelegate,UIScrollViewDelegate>
{
    UIView * _blackView;
    BOOL _bChange;
}

@end

@implementation PhotoView

-(UIImageView *)photo
{
    if (_photo == nil) {
        _photo = [[UIImageView alloc]initWithFrame:CGRectMake(0, (ViewHeight-ViewWidth)/2.0, ViewWidth, ViewWidth)];
    }
    return _photo;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self scrollViewSet];
        [self createTouchView:frame];
        [self createImageView:frame];
        [self createTap];
    }
    return self;
}

- (void)setLocaStr:(NSString *)locaStr
{
    _locaStr = locaStr;
    self.photo.image = [UIImage imageNamed:locaStr];
}

- (void)setImageURL:(NSString *)imageURL
{
    _imageURL = imageURL;
    [_photo sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"deaful.png"] options:SDWebImageCacheMemoryOnly progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        double size = expectedSize>=0?(double)expectedSize:-expectedSize;
        
        if (_percentBlock) {
            _percentBlock(receivedSize/size);
        }
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        if (self.loadFinish) {
            _loadFinish(YES,error);
        }
    }];
}

#pragma mark - create
- (void)scrollViewSet
{
    self.backgroundColor = [UIColor blackColor];
    self.delegate = self;
    self.pagingEnabled = NO;
    self.minimumZoomScale=1.0f;
    self.maximumZoomScale=2.0f;
    _bChange = NO;
}

- (void)createTouchView:(CGRect)frame
{
    _blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
    _blackView.backgroundColor = [UIColor blackColor];
    [self addSubview:_blackView];
}

- (void)createTap
{
    //单指双击
    UITapGestureRecognizer * oneFingerTwoClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoClickAction:)];
    oneFingerTwoClick.numberOfTouchesRequired = 1;
    oneFingerTwoClick.numberOfTapsRequired = 2;
    oneFingerTwoClick.delegate = self;
    [self addGestureRecognizer:oneFingerTwoClick];
    
    //单指单击
    UITapGestureRecognizer * oneFingerOneClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneClickAction:)];
    oneFingerOneClick.numberOfTouchesRequired = 1;
    oneFingerOneClick.numberOfTapsRequired = 1;
    oneFingerOneClick.delegate = self;
    [self addGestureRecognizer:oneFingerOneClick];
    
    [oneFingerOneClick requireGestureRecognizerToFail:oneFingerTwoClick];//确立优先级
}

- (void)createImageView:(CGRect)frame
{
    self.photo.userInteractionEnabled = YES;
    [self addSubview:_photo];
}

#pragma mark - scrollView Delegate
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    for (id view in [self subviews]) {
        if ([view isKindOfClass:[UIImageView class]]) {
            return view;
        }
    }
    return  nil;
}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    //调整位置
    CGFloat gap = (ViewHeight-view.frame.size.width)/2.0;
    gap = gap >0? gap:0;
    _bChange = gap == (ViewHeight-ViewWidth)/2.0? NO:YES;
    [UIView animateWithDuration:0.1 animations:^{
        view.frame = CGRectMake(0, gap, view.frame.size.width, view.frame.size.width);
    }];
}

#pragma mark - btnAction
-(void)twoClickAction:(id)sender
{
    self.zoomScale = (_bChange)?1:2;
    CGFloat gap = (ViewHeight-_photo.frame.size.width)/2.0;
    gap = gap >0? gap:0;
    [UIView animateWithDuration:0.3 animations:^{
        _photo.frame = CGRectMake(0, gap, _photo.frame.size.width, _photo.frame.size.width);
    }];
    _bChange = !_bChange;
}

-(void)oneClickAction:(id)sender
{
    if (_photoDelegate && [_photoDelegate respondsToSelector:@selector(oneFingerOneClick:)]) {
        [_photoDelegate oneFingerOneClick:self.tag];
    }
}

@end

多张照片的时候 分为网络加载 本地图片两种。加载图片使用SDWebImage

#import <UIKit/UIKit.h>
#import "PhototDefinde.h"

@protocol PhotoScrollDelegate <NSObject>
@optional
- (void)oneFingerOneClick:(NSInteger)index;//第几页触发单击

@end

@interface PhotoScrollView : UIView

@property (nonatomic, strong)NSArray * localArr;                //本地图片 图片名
@property (nonatomic, strong)NSArray * loadArr;                 //网络图片 url
@property (nonatomic, weak)id<PhotoScrollDelegate> delegate;
@property (nonatomic, assign)NSInteger progressStyle;         //进度条样式

@property (nonatomic, assign)BOOL bClearCache;                  //是否清除内存内缓存 默认不清除
@end


#import "PhotoScrollView.h"
#import "PhotoView.h"
#import "XJScrollView.h"
#import "XJProgress.h"

@interface PhotoScrollView()<PhotoViewDelegate>
{

}

@property (nonatomic, strong)XJScrollView * scrollView;
@end

@implementation PhotoScrollView

-(XJScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[XJScrollView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
    }
    return _scrollView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
    }
    return self;
}

//本地
- (void)setLocalArr:(NSArray *)localArr
{
    _localArr = localArr;
    self.scrollView.pageNum = localArr.count;
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self addSubview:self.scrollView];
    for (int i = 0; i < localArr.count; i++) {
        PhotoView * view = [[PhotoView alloc]initWithFrame:CGRectMake(ViewWidth*i, 0, ViewWidth, ViewHeight)];
        view.pageNum = 1;
        view.tag = 1000+i;
        view.locaStr = localArr[i];
        view.photoDelegate = self;
        [_scrollView addSubview:view];
    }
}

//网络
- (void)setLoadArr:(NSArray *)loadArr
{
    _loadArr = loadArr;
    self.scrollView.pageNum = loadArr.count;
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self addSubview:self.scrollView];
    for (int i = 0; i < loadArr.count; i++) {
        PhotoView * view = [[PhotoView alloc]initWithFrame:CGRectMake(ViewWidth*i, 0, ViewWidth, ViewHeight)];
        view.pageNum = 1;
        view.tag = 1000+i;
        view.photoDelegate = self;
        [_scrollView addSubview:view];
        
        XJProgress * progress = [[XJProgress alloc]initWithFrame:CGRectMake((view.frame.size.width-70)/2.0, (view.frame.size.height-70)/2.0, 70, 70)];
        progress.tag = 20000+i;
        progress.progressType = _progressStyle;
        progress.precent = 0;
        [view addSubview:progress];
        
        //对进度条处理
        view.percentBlock = ^(double percent){
            progress.precent = percent;
        };
        
        view.loadFinish = ^(BOOL finish,NSError * error)
        {
            if (finish) {
                [progress removeFromSuperview];
            }
        };
        
        //实现要在调用之前
        view.imageURL = loadArr[i];
    }
}

#pragma mark - Delegate
-(void)oneFingerOneClick:(NSInteger)tag
{
    if (_delegate && [_delegate respondsToSelector:@selector(oneFingerOneClick:)]) {
        [_delegate oneFingerOneClick:tag-1000];
    }
}

- (void)dealloc
{
    if (_bClearCache) {
        [[SDImageCache sharedImageCache] clearMemory];
    }
}

定义下浏览图片时候的进度条

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, ProgressType) {
    Progress_circular = -1,         //圆型
    Progress_default = 0,           //默认、
    Progress_noProgress = 2,        //无进度条
    Progress_chrysanthemum = 3,     //小菊花
    Process_gif = 4,                //GIF
};

@interface XJProgress : UIView

@property (nonatomic, assign)double precent;
@property (nonatomic, assign)ProgressType progressType;

@end


#import "XJProgress.h"
#import "PhototDefinde.h"
#import "GifView.h"

@interface XJProgress()
{

}

@property (nonatomic, strong)UILabel * percentLabel;
@property (nonatomic, strong)UIProgressView * progressView;
@property (nonatomic, strong)GifView * gifView;

@end

@implementation XJProgress

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

#pragma mark - darw
- (void)drawRect:(CGRect)rect {
    if (_progressType == Progress_circular) {
        
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGPoint center = CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0);//设置圆的中心
        CGFloat radius = 25;    //半径
        CGFloat startA = - M_PI_2;  //圆起点位置
        CGFloat endA = -M_PI_2 + M_PI * 2 * _precent;  //圆终点位置
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
        CGContextSetLineWidth(ctx, 10); //设置线条宽度
        [[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1] setStroke]; //设置描边颜色
        CGContextAddPath(ctx, path.CGPath); //把路径添加到上下文
        CGContextStrokePath(ctx);  //渲染
    }
}

#pragma mark - GetSet
- (void)setProgressType:(ProgressType)progressType
{
    _progressType = progressType;
    if (_progressType == Progress_circular) {
        
        [self addSubview:self.percentLabel];
        
    }else if (_progressType == Progress_default){
        
        [self addSubview:self.progressView];
        self.percentLabel.frame = CGRectMake(0, CGRectGetMaxY(_progressView.frame)+5, ViewWidth, 30);
        _percentLabel.textColor = [UIColor grayColor];
        _percentLabel.layer.cornerRadius = 0;
        _percentLabel.backgroundColor = [UIColor clearColor];
        _percentLabel.alpha = 1;
        [self addSubview:_percentLabel];
        
    }else if (_progressType == Progress_chrysanthemum)
    {
        [self addSubview:[self createChrysanthemumView]];
    }else if (_progressType == Process_gif)
    {
        self.gifView.gifName = @"loadImage";
        [self addSubview:_gifView];
    }
}

- (void)setPrecent:(double)precent
{
    _precent = precent;
    if (_progressType == Progress_circular) {
        
        _percentLabel.text = [NSString stringWithFormat:@"%.0f%%",precent*100.0];
        [self setNeedsDisplay];
        
    }else if (_progressType == Progress_default){
        
        _progressView.progress = precent;
        _percentLabel.text = [NSString stringWithFormat:@"%.0f%%",precent*100.0];
        
    }else if (_progressType == Progress_chrysanthemum)
    {
        
    }else if (_progressType == Process_gif){
    }
}

#pragma mark - lazyLoad
-(UILabel *)percentLabel
{
    if (_percentLabel == nil) {
        _percentLabel = [[UILabel alloc]initWithFrame:CGRectMake((ViewWidth-40)/2.0, (ViewHeight-40)/2.0, 40, 40)];
        _percentLabel.font = [UIFont boldSystemFontOfSize:12.0f];
        _percentLabel.textColor = [UIColor darkTextColor];
        _percentLabel.textAlignment = NSTextAlignmentCenter;
        _percentLabel.backgroundColor = [UIColor whiteColor];
        _percentLabel.alpha = 0.8;
        _percentLabel.clipsToBounds = YES;
        _percentLabel.layer.cornerRadius = 20;
    }
    return _percentLabel;
}

-(UIProgressView *)progressView
{
    if (_progressView == nil) {
        _progressView = [[UIProgressView alloc]initWithFrame:CGRectMake((ViewWidth-150)/2.0, (ViewHeight-2)/2.0, 150, 2)];
        _progressView.progressViewStyle = UIProgressViewStyleDefault;
    }
    return _progressView;
}

-(GifView *)gifView
{
    if (_gifView == nil) {
        _gifView = [[GifView alloc]initWithFrame:CGRectMake((ViewWidth-65)/2.0, (ViewHeight-65)/2.0, 65, 65)];
    }
    return _gifView;
}
#pragma mark - create
- (UIView*)createChrysanthemumView
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake((ViewWidth-50)/2.0, (ViewHeight-50)/2.0, 50, 50)];
    view.layer.cornerRadius = 4;
    view.backgroundColor = [UIColor blackColor];
    
    UIActivityIndicatorView * indicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake((view.frame.size.width-20)/2.0, (view.frame.size.height-20)/2.0, 20, 20)];
    [view addSubview:indicator];
    
    [indicator startAnimating];
    return view;
}

整个写好了 下回用的时候就很方便了

PhotoScrollView * view = [[PhotoScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64)];
    view.delegate = self;
    view.bClearCache = YES;
    view.progressStyle = -1;
    view.loadArr = @[@"http://pic8.nipic.com/20100728/4800623_123729985383_2.jpg",@"http://pic8.nipic.com/20100728/4800623_123729985383_2.jpg",@"http://p4.gexing.com/shaitu/20130403/1949/515c17385f7c4.jpg"];
    [self.view addSubview:view];

//单指触摸屏幕的时候触发
- (void)oneFingerOneClick:(NSInteger)index
{
    NSLog(@"第%ld页单指触发",index+1);
}

下载:https://pan.baidu.com/s/1nu8Dyjf

© 著作权归作者所有

langzhouzhou1
粉丝 5
博文 131
码字总数 63910
作品 0
杭州
私信 提问
友锋电子相册制作 V9.2 官方版

  友锋电子相册制作官方版可以方便的将您的照片制作成电子相册,并可对电子相册设置密码,方便您收藏、管理大量相片。   友锋电子相册制可以轻松制作生成Flash影片、也可以生成可独立运行...

zgxahxm
2018/03/21
0
0
微信音乐相册制作微信场景制作方法?源码?

现在智能手机非常的发达,许多网友都有希望在手机上分享音乐相册的想法,但是往往很多音乐相册的教程都不适应制作手机相册,微信相册,所以这篇文章特意介绍如何制作一个漂亮的微信音乐相册。...

我是一个小小草
2015/11/21
178
1
电子相册&照片书制作系统优化

【业务需求】 一、功能需求 已有类似网易印象派(http://yxp.163.com/)、fun秀 (http://fun.qq.com)的相册(照片书)DIY制作系统,并已经完成了和已有业务系统、阿里云oss存储的对接开发工...

zjzy73
2017/06/29
24
0
电子相册&照片书制作系统优化

【业务需求】 一、功能需求 已有类似网易印象派(http://yxp.163.com/)、fun秀 (http://fun.qq.com)的相册(照片书)DIY制作系统,并已经完成了和已有业务系统、阿里云oss存储的对接开发工...

zjzy73
2017/06/29
38
0
使用H5实现电子相册【照片书】制作系统

【业务需求】 一、功能需求 类似网易印象派(http://yxp.163.com/)、fun秀 (http://fun.qq.com)的相册(照片书)DIY制作系统,需要和已有业务系统、阿里云oss存储的对接开发工作,支持制作...

zjzy73
2017/06/29
70
0

没有更多内容

加载失败,请刷新页面

加载更多

Centos7 安装zabbix-agent

rpm -i https://repo.zabbix.com/zabbix/4.2/rhel/6/x86_64/zabbix-release-4.2-2.el6.noarch.rpm 可以到https://repo.zabbix.com/zabbix找到对应的版本 yum install zabbix-agent -y 出现E......

abowu
昨天
8
0
文本编辑器GNU nano 4.4 发布

GNU nano 4.4 "Hagelslag" 更新日志: 启动时,光标可以放在第一个或最后一个出现位置 字符串前面带有+/string 或 +?string的字符串。 发生自动硬包装时((--breaklonglines),任何前导引号...

linuxCool
昨天
7
0
你知道字节序吗

字节序 最近在调一个自定义报文的接口时,本来以为挺简单的,发现踩了好几个坑,其中一个比较“刻骨铭心”的问题就是数据的字节序问题。 背景 自定义报文,调用接口,服务端报文解析失败 iO...

杭城小刘
昨天
3
0
设计模式之依赖倒置原则

方法

东风破2019
昨天
6
0
关于如何通过模拟器完成模拟步数提升傻瓜式解决方案(囧)

因为对Android开发不太了解,也没去问朋友所以误打误撞找到的一个提升步数的解决方案,当然只是针对某安APP运动RUN的解决方式吧。 对Android不太了解,所以找了很多的解决方案来看看能不能破...

华山猛男
昨天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部