前言
公司項目有很多地方要用到圖片上傳功能箱锐,還有上傳的歷史記錄比勉,這方面就有需要預(yù)覽這個需求啦,之前做的都沒有這個功能驹止,于是夏夏自己寫了個簡易的預(yù)覽浩聋。。臊恋。
由于是模擬器衣洁,沒有辦法把放大,縮小等功能展示出來抖仅。
思路
1.只是簡單的實現(xiàn)下預(yù)覽的功能坊夫,于是夏夏只是單純的寫了個view用于展示,一般的預(yù)覽都是全屏展示的撤卢,而且長按的時候要能保存圖片环凿,也就是說要present
出sheet
出來,于是夏夏就添加到NavigationController
的View
上了凸丸。當(dāng)然拷邢,為了美觀,夏夏也是加了個專場動畫~ show code 思密達:
UIImageView *imageView = (UIImageView *)tap.view;
UIImage *image = imageView.image;
ReViewPhotoView *review = [[ReViewPhotoView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) Photo:image];
CATransition *transition = [CATransition animation];
transition.type = kCATransitionReveal;
transition.duration = 0.5;
[review.layer addAnimation:transition forKey:nil];
[self.navigationController.view addSubview:review];
2.先給一個整個window
的背景View
屎慢,再加個展現(xiàn)照片的imageView
瞭稼。一般圖片都是劇中顯示,每個圖片都是帶有size
屬性腻惠,可以根據(jù)圖片原有的size
和屏幕款高比环肘,從而寫出展示的imageView
的frame
。
CGFloat scale = photo.size.width / self.frame.size.width;
CGFloat height = photo.size.height / scale;
self.backgroundColor = [UIColor blackColor];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, (self.frame.size.height - height)/2, self.frame.size.width, height)];
imageView.image = photo;
imageView.userInteractionEnabled = YES;
[self addSubview:imageView];
3.接下來就是給imageView
加手勢了集灌,加上了各種手勢才能有各種交互的功能呀~
先加一個可以放大的功能思密達~~
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchChangeScale:)];
[imageView addGestureRecognizer:pinchGesture];
- (void)pinchChangeScale:(UIPinchGestureRecognizer *)pinch {
UIImageView *imageView = (UIImageView *)pinch.view;
if (pinch.state == UIGestureRecognizerStateBegan) {
lastScare = 1.0;
}
CGFloat scare = 1 - (lastScare - pinch.scale);
CGAffineTransform currentTransform = imageView.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scare, scare);
[imageView setTransform:newTransform];
lastScare = pinch.scale;
}
當(dāng)然咯悔雹,要是能放大肯定是要能拖動查看的說~
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[imageView addGestureRecognizer:panGesture];
- (void)move:(UIPanGestureRecognizer *)pan {
CGPoint transpoint = [pan translationInView:self];
UIImageView *imageView = (UIImageView *)pan.view;
if (pan.state == UIGestureRecognizerStateBegan) {
firstX = imageView.center.x;
firstY = imageView.center.y;
}
if (lastScare == 1.0f) {
return;
}
transpoint = CGPointMake(firstX+transpoint.x, firstY+transpoint.y);
[imageView setCenter:transpoint];
}
唔~還要有一個雙擊可以返回原來的狀態(tài)手勢复哆!
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapResolve:)];
doubleTapGesture.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:doubleTapGesture];
- (void)tapResolve:(UITapGestureRecognizer *)tap {
UIImageView *imageView = (UIImageView *)tap.view;
[imageView setTransform:transform];
[imageView setFrame:imgFrame];
lastScare = 1.0f;
}
但是還需要一個單擊返回的手勢,雙擊和單擊會沖突腌零,這個時候可以使用這個函數(shù)requireGestureRecognizerToFail
梯找。
最后如果是網(wǎng)上的圖片預(yù)覽,可能需要長按本地保存的功能益涧,可以這樣寫喲:
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
UIImageView *imageView = (UIImageView *)longPress.view;
if (longPress.state == UIGestureRecognizerStateBegan) {
if (self.longpressblock && imageView.image) {
self.longpressblock(imageView.image);
}
}
}
本地保存圖片的方法是這個:UIImageWriteToSavedPhotosAlbum
,之所以用block是因為這只是個view锈锤,沒辦法present出UIAlertController
。
結(jié)束啦~
這就是一個簡單的圖片預(yù)覽闲询,demo的地址在這里:https://github.com/ioscick/-Demo
可以用于互相學(xué)習(xí)討論喲久免!