设为首页 加入收藏

TOP

iOS- XKZoomingView 简单的图片缩放预览,支持横屏、长图【手势:单击、双击、放大缩小】(二)
2019-08-31 00:18:34 】 浏览:83
Tags:iOS- XKZoomingView 简单 图片 缩放 预览 支持 长图 手势 单击 双击 放大 缩小
.size.height
= ratioHW * imageRect.size.width; imageRect.origin.x = 0; } else{ imageRect.size.width = _mainImage.size.width; imageRect.size.height = _mainImage.size.height; imageRect.origin.x = (scWidth - imageRect.size.width)/2; } ///height if (imageRect.size.height > scHeight) { imageRect.origin.y = 0; } else{ imageRect.origin.y = (scHeight - imageRect.size.height)/2; } return imageRect; } return CGRectZero; } /** 获取点击位置后所需的偏移量【目的是呈现点击位置在试图上】 @param location 点击位置 */ - (void)zoomingOffset:(CGPoint)location{ CGFloat lo_x = location.x * self.zoomScale; CGFloat lo_y = location.y * self.zoomScale; CGFloat off_x; CGFloat off_y; ///off_x if (lo_x < CGRectGetWidth(self.frame)/2) { off_x = 0; } else if (lo_x > self.contentSize.width - CGRectGetWidth(self.frame)/2){ off_x = self.contentSize.width - CGRectGetWidth(self.frame); } else{ off_x = lo_x - CGRectGetWidth(self.frame)/2; } ///off_y if (lo_y < CGRectGetHeight(self.frame)/2) { off_y = 0; } else if (lo_y > self.contentSize.height - CGRectGetHeight(self.frame)/2){ if (self.contentSize.height <= CGRectGetHeight(self.frame)) { off_y = 0; } else{ off_y = self.contentSize.height - CGRectGetHeight(self.frame); } } else{ off_y = lo_y - CGRectGetHeight(self.frame)/2; } [self setContentOffset:CGPointMake(off_x, off_y)]; } #pragma mark - 重置图片 - (void)resetImageViewState{ self.zoomScale = 1; _mainImage = nil;; self.mainImageView.image = nil; } #pragma mark - 变量 - (UIImageView *)mainImageView { if (!_mainImageView) { _mainImageView = [UIImageView new]; _mainImageView.image = nil; _mainImageView.contentMode = UIViewContentModeScaleAspectFit; _mainImageView.userInteractionEnabled = YES; } return _mainImageView; } #pragma mark - 单击 - (void)tapSingleSponse:(UITapGestureRecognizer *)singleTap{ if (!self.mainImageView.image) { return; } if (self.zoomScale != 1) { [UIView animateWithDuration:0.2 animations:^{ self.zoomScale = 1; } completion:^(BOOL finished) { [self setContentOffset:_currPont animated:YES]; }]; } } #pragma mark - 双击 - (void)tapDoubleSponse:(UITapGestureRecognizer *)doubleTap{ if (!self.mainImageView.image) { return; } CGPoint point = [doubleTap locationInView:self.mainImageView]; if (self.zoomScale == 1) { [UIView animateWithDuration:0.2 animations:^{ self.zoomScale = 2.0; [self zoomingOffset:point]; }]; } else{ [UIView animateWithDuration:0.2 animations:^{ self.zoomScale = 1; } completion:^(BOOL finished) { [self setContentOffset:_currPont animated:YES]; }]; } } #pragma mark - UIScrollViewDelegate - (void)scrollViewDidZoom:(UIScrollView *)scrollView { if (!self.mainImageView.image) { return; } CGRect imageViewFrame = self.mainImageView.frame; CGFloat width = imageViewFrame.size.width, height = imageViewFrame.size.height, sHeight = scrollView.bounds.size.height, sWidth = scrollView.bounds.size.width; if (height > sHeight) { imageViewFrame.origin.y = 0; } else { imageViewFrame.origin.y = (sHeight - height) / 2.0; } if (width > sWidth) { imageViewFrame.origin.x = 0; } else { imageViewFrame.origin.x = (sWidth - width) / 2.0; } self.mainImageView.
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS开发简记(7):网络请求模块 下一篇iOS证书配置与管理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目