|
| 1 | +// |
| 2 | +// DWOrigImgView.m |
| 3 | +// RCTAuroraIMUI |
| 4 | +// |
| 5 | +// Created by Dowin on 2017/8/29. |
| 6 | +// Copyright © 2017年 HXHG. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "DWOrigImgView.h" |
| 10 | +#import "UIView+Extend.h" |
| 11 | +#import "MyCacheImageView.h" |
| 12 | + |
| 13 | + |
| 14 | +@interface DWOrigImgView (){ |
| 15 | + MyCacheImageView *imgView; |
| 16 | + UIPinchGestureRecognizer *pinchGest; |
| 17 | + UIPanGestureRecognizer *panGest; |
| 18 | + UITapGestureRecognizer *tapGest; |
| 19 | + CGRect orginFrame; |
| 20 | + UIButton *downBtn; |
| 21 | + |
| 22 | +} |
| 23 | + |
| 24 | +@end |
| 25 | + |
| 26 | +@implementation DWOrigImgView |
| 27 | + |
| 28 | +- (void)dealloc{ |
| 29 | + [imgView removeGestureRecognizer:pinchGest]; |
| 30 | + [imgView removeGestureRecognizer:panGest]; |
| 31 | + [self removeGestureRecognizer:tapGest]; |
| 32 | +} |
| 33 | + |
| 34 | ++ (instancetype)origImgViewWithDict:(NSDictionary *)dict{ |
| 35 | + DWOrigImgView *orgView = [[DWOrigImgView alloc]init]; |
| 36 | + [orgView setupImgViewWithDict:dict]; |
| 37 | + return orgView; |
| 38 | +} |
| 39 | + |
| 40 | +- (void)setupImgViewWithDict:(NSDictionary *)dict{ |
| 41 | + NSString *thumbPath = [dict objectForKey:@"thumbPath"]; |
| 42 | + NSString *strUrl = [dict objectForKey:@"url"]; |
| 43 | + UIImage *placeImg = [UIImage imageWithData:[NSData dataWithContentsOfFile:thumbPath]]; |
| 44 | + [imgView setImageURL:strUrl placeImage:placeImg]; |
| 45 | + NSNumber *numW = [dict objectForKey:@"imageWidth"]; |
| 46 | + NSNumber *numH = [dict objectForKey:@"imageHeight"]; |
| 47 | + CGFloat multiple = numW.floatValue / numH.floatValue; |
| 48 | + CGFloat imgW = screenW; |
| 49 | + CGFloat imgH = imgW / multiple; |
| 50 | + CGFloat imgY = 0; |
| 51 | + if (imgH < screenH) { |
| 52 | + imgY = (screenH-imgH)*0.5; |
| 53 | + } |
| 54 | + imgView.frame = CGRectMake(0, imgY, imgW, imgH); |
| 55 | + orginFrame = imgView.frame; |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +- (instancetype)initWithFrame:(CGRect)frame{ |
| 60 | + if (self = [super initWithFrame:frame]) { |
| 61 | + self.backgroundColor = [UIColor blackColor]; |
| 62 | + self.userInteractionEnabled = YES; |
| 63 | + [self addContentView]; |
| 64 | + } |
| 65 | + return self; |
| 66 | +} |
| 67 | + |
| 68 | +- (void)addContentView{ |
| 69 | + imgView = [[MyCacheImageView alloc]init]; |
| 70 | + imgView.contentMode = UIViewContentModeScaleAspectFit; |
| 71 | + imgView.userInteractionEnabled = YES; |
| 72 | + imgView.multipleTouchEnabled = YES; |
| 73 | + [self addSubview:imgView]; |
| 74 | + pinchGest = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(clickPinchView:)]; |
| 75 | + [imgView addGestureRecognizer:pinchGest]; |
| 76 | + panGest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(clickPanView:)]; |
| 77 | + [imgView addGestureRecognizer:panGest]; |
| 78 | + |
| 79 | + tapGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTapGest)]; |
| 80 | + [self addGestureRecognizer:tapGest]; |
| 81 | + |
| 82 | + CGFloat btnWH = 35; |
| 83 | + CGFloat btnX = screenW - btnWH - 20; |
| 84 | + CGFloat btnY = screenH - btnWH - 20; |
| 85 | + downBtn = [[UIButton alloc]initWithFrame:CGRectMake(btnX, btnY, btnWH, btnWH)]; |
| 86 | + [downBtn setBackgroundImage:[UIImage imageNamed:@"download"] forState:UIControlStateNormal]; |
| 87 | + [downBtn addTarget:self action:@selector(clickDownLoadBtn) forControlEvents:UIControlEventTouchUpInside]; |
| 88 | + [self addSubview:downBtn]; |
| 89 | + [self performSelector:@selector(delayMethod) withObject:nil afterDelay:3.0]; |
| 90 | + |
| 91 | +} |
| 92 | + |
| 93 | +- (void)delayMethod{ |
| 94 | + [UIView animateWithDuration:0.5 animations:^{ |
| 95 | + downBtn.alpha -= 1; |
| 96 | + } completion:^(BOOL finished) { |
| 97 | + downBtn.hidden = YES; |
| 98 | + downBtn.alpha = 1; |
| 99 | + }]; |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | +- (void)clickDownLoadBtn{ |
| 104 | + UIImageWriteToSavedPhotosAlbum(imgView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); |
| 105 | +} |
| 106 | + |
| 107 | +- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo{ |
| 108 | + NSString *msg = nil ; |
| 109 | + if(error != NULL){ |
| 110 | + msg = @"保存图片失败" ; |
| 111 | + }else{ |
| 112 | + msg = @"保存图片成功" ; |
| 113 | + } |
| 114 | + UIAlertView *alert = [[UIAlertView alloc] initWithTitle:msg message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; |
| 115 | + [alert show]; |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +- (void)clickPinchView:(UIPinchGestureRecognizer *)gest{ |
| 120 | + UIView *view = gest.view; |
| 121 | + if (gest.state == UIGestureRecognizerStateBegan || gest.state == UIGestureRecognizerStateChanged) { |
| 122 | + view.transform = CGAffineTransformScale(view.transform, gest.scale, gest.scale); |
| 123 | + gest.scale = 1; |
| 124 | + } |
| 125 | + if (gest.state == UIGestureRecognizerStateEnded) { |
| 126 | + if (view.frame.size.width < screenW) { |
| 127 | + CGFloat tmpW = screenW - view.width; |
| 128 | + CGFloat tmpH = orginFrame.size.height - view.height; |
| 129 | + [UIView animateWithDuration:0.3 animations:^{ |
| 130 | + view.width += tmpW; |
| 131 | + view.height += tmpH; |
| 132 | + view.x -= tmpW*0.5; |
| 133 | + view.y -= tmpH*0.5; |
| 134 | + |
| 135 | + } completion:^(BOOL finished) { |
| 136 | + view.frame = orginFrame; |
| 137 | + }]; |
| 138 | + } |
| 139 | + if (view.width > 1.5 * screenW) { |
| 140 | + CGFloat maxH = view.height - orginFrame.size.height*1.5; |
| 141 | + CGFloat maxW = view.width - screenW*1.5; |
| 142 | + [UIView animateWithDuration:0.3 animations:^{ |
| 143 | + view.width -= maxW; |
| 144 | + view.height -= maxH; |
| 145 | + view.x += maxW*0.5; |
| 146 | + view.y += maxH*0.5; |
| 147 | + }]; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +- (void)clickPanView:(UIPanGestureRecognizer *)gest{ |
| 153 | + UIView *view = gest.view; |
| 154 | + if (gest.state == UIGestureRecognizerStateBegan || gest.state == UIGestureRecognizerStateChanged) { |
| 155 | + CGPoint translation = [gest translationInView:view.superview]; |
| 156 | + [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}]; |
| 157 | + [gest setTranslation:CGPointZero inView:view.superview]; |
| 158 | + } |
| 159 | + if (gest.state == UIGestureRecognizerStateEnded) { |
| 160 | + if (view.x>0) { |
| 161 | + CGFloat tmpX = view.x; |
| 162 | + [UIView animateWithDuration:0.3 animations:^{ |
| 163 | + view.x -= tmpX; |
| 164 | + } completion:^(BOOL finished) { |
| 165 | + view.x = 0; |
| 166 | + }]; |
| 167 | + }else if((screenW-view.x)>view.width){ |
| 168 | + CGFloat tmpx = screenW - view.x - view.width; |
| 169 | + [UIView animateWithDuration:0.3 animations:^{ |
| 170 | + view.x += tmpx; |
| 171 | + } completion:^(BOOL finished) { |
| 172 | + view.x = screenW - view.width; |
| 173 | + }]; |
| 174 | + } |
| 175 | + if (view.height > screenH) { |
| 176 | + if ((view.height - screenH + view.y)<0) { |
| 177 | + CGFloat tmpH = screenH - view.height - view.y; |
| 178 | + [UIView animateWithDuration:0.3 animations:^{ |
| 179 | + view.y += tmpH; |
| 180 | + }]; |
| 181 | + }else if (view.y > 0){ |
| 182 | + CGFloat tmpH = view.y ; |
| 183 | + [UIView animateWithDuration:0.3 animations:^{ |
| 184 | + view.y -= tmpH; |
| 185 | + }]; |
| 186 | + } |
| 187 | + }else{ |
| 188 | + CGFloat tmpH = view.y - (screenH - view.height)*0.5;; |
| 189 | + [UIView animateWithDuration:0.3 animations:^{ |
| 190 | + view.y -= tmpH; |
| 191 | + }]; |
| 192 | + } |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +- (void)clickTapGest{ |
| 197 | + [self removeFromSuperview]; |
| 198 | +} |
| 199 | + |
| 200 | +@end |
0 commit comments