self.view 上,继而就可以很方便的显示 UIAlertController 了:
- (void)previewFromImageView:(UIImageView *)fromImageViewinContainer:(UIView *)container {
_fromImageView = fromImageView;
fromImageView.hidden = YES;
[containeraddSubview:self]; // 将 CYPhotoPreviewer 添加到 container 上
self.containerView.origin = CGPointZero;
self.containerView.width = self.width; // containerView 的宽度是屏幕的宽度
UIImage *image = fromImageView.image;
// 计算 containerView 的高度
if (image.size.height / image.size.height > self.height / self.width) {
self.containerView.height = floor(image.size.height / (image.size.width / self.width));
} else {
CGFloatheight = image.size.height / image.size.width * self.width;
if (height self.height && self.containerView.height - self.height
可以看到,我们将外面的图片 fromImageView 传递进来,是为了显示更好的动画效果;将控制器的 container(self.view)传递进来,是为了将 CYPhotoPreviewer 添加到 container 的细节不需要在调用处处理,即初始化 CYPhotoPreviewer 之后,CYPhotoPreviewer 就直接被 container 添加为 subview 了。动画很简单不再细说。
显示的效果已经做好,单击关闭 CYPhotoPreviewer 也比较好实现,只需要从父类移除 CYPhotoPreviewer 即可:
- (void)dismiss {
[UIViewanimateWithDuration:0.18 delay:0.0 options:UIViewAnimationOptionCurveEaseInOutanimations:^{
CGRectfromRect = [self.fromImageViewconvertRect:self.fromImageView.boundstoView:self.containerView];
self.imageView.contentMode = self.fromImageView.contentMode;
self.imageView.frame = fromRect;
self.blurBackground.alpha = 0.01;
} completion:^(BOOL finished) {
[UIViewanimateWithDuration:0.10 delay:0 options:UIViewAnimationOptionCurveEaseInOutanimations:^{
self.fromImageView.hidden = NO;
self.alpha = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}];
}
好了,显示和关闭 CYPhotoPreviewer 都实现了,如果需要双击缩放图片效果,就得实现 UIScrollViewDelegate 的两个方法以及 CYPhotoPreviewer 的双击手势:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.containerView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
UIView *subView = self.containerView;
CGFloatoffsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
(scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
CGFloatoffsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
(scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
subView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
scrollView.contentSize.height * 0.5 + offsetY);
}
- (void)doubleTap:(UITapGestureRecognizer *)recognizer {
if (self.scrollView.zoomScale > 1.0) {
[self.scrollViewsetZoomScale:1.0 animated:YES];
} else {
CGPointtouchPoint = [recognizerlocationInView:self.imageView];
CGFloatnewZoomScale = self.scrollView.maximumZoomScale;
CGFloatxSize = self.width / newZoomScale;
CGFloatySize = self.height / newZoomScale;
[self.scrollViewzoomToRect:CGRectMake(touchPoint.x - xSize / 2, touchPoint.y - ySize / 2, xSize, ySize) animated:YES];
}
}










