UIScroll 中心图像

Posted

技术标签:

【中文标题】UIScroll 中心图像【英文标题】:UIScroll center image 【发布时间】:2013-08-15 05:18:43 【问题描述】:

我已经多次编辑了这个问题,但仍然无法让我的图像在 uiview 中居中。我希望它们能够像照片应用程序一样旋转,并在用户打开它们时显示正确的尺寸。这是我正在使用的:

在我的 PhotoViewController.h 中

#import <UIKit/UIKit.h>

@protocol PhotoViewControllerDelegate <NSObject>

- (void)toggleChromeDisplay;

@end


@interface PhotoViewController : UIViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate>


@property (nonatomic, strong) UIImage *photo;
@property (nonatomic) NSUInteger num;


//Delegate
@property (nonatomic, strong) id<PhotoViewControllerDelegate> photoViewControllerDelegate;

@property (nonatomic, strong) UIImageView *photoImgView;
@property (nonatomic, strong) UIScrollView *scrollView;



@end

在我的 PhotoViewController.m 中:

#import "PhotoViewController.h"

@interface PhotoViewController ()

@end

@implementation PhotoViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        //todo

    
    return self;


- (void)viewDidLoad

    [super viewDidLoad];
    CGRect screenBounds = self.view.bounds;


    //scroll view
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height)];
    _scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    _scrollView.pagingEnabled = NO;
    _scrollView.scrollEnabled = YES;
    [_scrollView setBackgroundColor:[UIColor blueColor]];

    //Zoom Properties
    _scrollView.maximumZoomScale = 6.0;
    _scrollView.minimumZoomScale = 1.0;
    _scrollView.bouncesZoom = YES;
    _scrollView.delegate = self;
    _scrollView.zoomScale = 1.0;
    _scrollView.contentSize = _photoImgView.bounds.size;
    [_scrollView setShowsHorizontalScrollIndicator:NO];
    [_scrollView setShowsVerticalScrollIndicator:NO];
    [self photoBounds];


    [self.view addSubview: _scrollView];


    //Add the UIImageView
    _photoImgView = [[UIImageView alloc] initWithImage:_photo];
    _photoImgView.image = _photo;
    _photoImgView.clipsToBounds = YES;
    _photoImgView.contentMode = UIViewContentModeScaleAspectFit;
    _photoImgView.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin);
    [_photoImgView setUserInteractionEnabled:YES];

    [_scrollView addSubview: _photoImgView];


    //Set up Gesture Recognizer
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
    UITapGestureRecognizer *dTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dTapGestureCaptured:)];

    dTap.numberOfTapsRequired = 2;
    [singleTap requireGestureRecognizerToFail:dTap];

    //Gesture Methods
    [self.scrollView addGestureRecognizer:singleTap];
    [self.scrollView addGestureRecognizer : dTap];






- (void)photoBounds


    UIInterfaceOrientation statusbar = [[UIApplication sharedApplication] statusBarOrientation];
    CGSize photoBounds = _photo.size;
    CGSize scrollBounds = self.view.bounds.size;
    CGRect frameToCenter = [_photoImgView frame];

    float newHeight = (scrollBounds.width / photoBounds.width) * photoBounds.height;
    float newWidth = (scrollBounds.height / photoBounds.height) * photoBounds.width;
    float yDist = fabsf(scrollBounds.height - newHeight) / 2;
    float xDist = fabsf(scrollBounds.width - newWidth) / 2;


    //Width Larger
    if (photoBounds.width >=photoBounds.height) 

        NSLog(@"portrait width");
        _photoImgView.frame = CGRectMake(0, 0, scrollBounds.width, newHeight);
        frameToCenter.origin.y = yDist;

    

    //Height Larger
    else if (photoBounds.height > photoBounds.width) 

        NSLog(@"portrait height");
        _photoImgView.frame = CGRectMake(0, 0, newWidth, scrollBounds.height);
        frameToCenter.origin.x = xDist;

    

    //Square
    else 

        NSLog(@"portrait square");
        if ((statusbar == 1) || (statusbar == 2)) 
            _photoImgView.frame = CGRectMake(0, 0, scrollBounds.width, newHeight);
            frameToCenter.origin.y = yDist;

         else 

            _photoImgView.frame = CGRectMake(0, 0, newWidth, scrollBounds.height);
            frameToCenter.origin.x = xDist;
        

    






//Rotation Magic
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

    //later


- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

    [self photoBounds];


- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

    //


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    return YES;




//Zoom Ability
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

    return self.photoImgView;


- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale

    NSLog(@"scale %f", scale);
    NSLog(@"done zooming");








//Touches Control
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture

    //CGPoint touchPoint=[gesture locationInView:_scrollView];
    NSLog(@"touched");
    NSLog(@"single touch");
    [self performSelector:@selector(callingHome) withObject:nil afterDelay:0];



- (void)dTapGestureCaptured:(UITapGestureRecognizer *)gesture


    NSLog(@"double touched");


- (void)panGestureCaptured:(UIPanGestureRecognizer *)gesture


    NSLog(@"pan gesture");




- (void)callingHome 

@end

总体问题是我无法让我的图片正确显示并且只能放大它,它周围没有空间,并且它需要在加载时具有正确的尺寸。我已经为此苦苦挣扎了几天。

有什么帮助吗?

【问题讨论】:

UIScrollView 可以自己处理平移手势。为什么你的集合是_scrollView.scrollEnabled = NO; 实际设置的内容大小是多少?看起来你可能没有设置它。 Mb 你的gestureRecognizers 会干扰本机scrollView gestureRecognizers... @liuyaodong 这绝对是我的问题的一部分,知道如何设置它以便我只能放大图像而不是整个框架吗? 阅读本教程,它会教你需要什么:raywenderlich.com/10518/… 【参考方案1】:

我已经使用我自己的一个非常简单的 UIScrollView 子类解决了类似的问题。您可以在这里查看它 - https://gist.github.com/ShadeApps/5a29e1cea3e1dc3df8c8。对我来说就像魅力一样。

这就是你的初始化方式:

scrollViewMain.delegate = self;
scrollViewMain.minimumZoomScale = 1.0;
scrollViewMain.maximumZoomScale = 3.0;
scrollViewMain.contentSize = imageViewMain.frame.size;
scrollViewMain.backgroundColor = [UIColor blackColor];
scrollViewMain.tileContainerView = imageViewMain;

【讨论】:

太棒了!我回家后看看。 @lostAstronaut 这再简单不过了——您将tileContainerView 属性指定为您的UIImageView,然后就可以了。然后,您将一个子类滚动视图添加到您的视图中,它应该已经为您准备好了。查看我的编辑。 哇,比我想象的要容易得多。非常感谢。【参考方案2】:

Peter Steinberger 有一篇关于这个问题的精彩博文:http://petersteinberger.com/blog/2013/how-to-center-uiscrollview/

【讨论】:

以上是关于UIScroll 中心图像的主要内容,如果未能解决你的问题,请参考以下文章

如何将 iScroll4 与 SwipeView 一起使用?

何时使用 UITouch 与 UIScroll

可以使用带有覆盖 div 的 iscroll 捏合/缩放吗?

UIScroll 及其嵌套元素

UIScroll 视图 + UIView + UITableview 问题

UIScroll 视图未按预期工作