iOS之性能优化技术日常iOS之基础知识

ios17~UICollectionViewCell中用UISc

2018-05-30  本文已影响187人  摹喵居士

2018.06.01

UICollectionViewCell中添加UIScrollView,使用UIImageView来缩放图片

self.scrollView.userInteractionEnabled = YES;
self.scrollView.scrollEnabled = YES;
self.scrollView.bounces = YES;
self.scrollView.maximumZoomScale = 3.0f;
self.scrollView.minimumZoomScale = 1.0f;
self.scrollView.delegate = self;
self.scrollView.clipsToBounds = YES;
[self.scrollView setZoomScale:self.scrollView.minimumZoomScale animated:NO];
- (void)onDoubleTappedGestureRecognizer:(UITapGestureRecognizer *)recognizer {
    

    if (self.scrollView.zoomScale > self.scrollView.minimumZoomScale) {
        // 已经放大 现在缩小
        [self.scrollView setZoomScale:self.scrollView.minimumZoomScale animated:YES];
    }
    else {
        [self.scrollView setZoomScale:self.scrollView.maximumZoomScale animated:YES];
    }

}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    CGFloat scrollW = CGRectGetWidth(scrollView.frame);
    CGFloat scrollH = CGRectGetHeight(scrollView.frame);
    
    CGSize contentSize = scrollView.contentSize;
    CGFloat offsetX = scrollW > contentSize.width ? (scrollW - contentSize.width) * 0.5 : 0;
    CGFloat offsetY = scrollH > contentSize.height ? (scrollH - contentSize.height) * 0.5 : 0;
    
    CGFloat centerX = contentSize.width * 0.5 + offsetX;
    CGFloat centerY = contentSize.height * 0.5 + offsetY;
    
    self.imageView.center = CGPointMake(centerX, centerY);
}
上一篇下一篇

猜你喜欢

热点阅读