UI滚动中心图像 [英] UIScroll center image

查看:23
本文介绍了UI滚动中心图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've edited this question a few times, but can still not get my images to center inside a uiview. I want them to be able to rotate like the photos app and display the correct size when a user brings them up. Here is what I'm working with:

在我的 PhotoViewController.h 中

In my 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 中:

In my 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

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

The overall issue is that I can not get my picture to display correctly and be able to zoom on just it, no space around it and It needs to be the correct dimensions on load. I've been struggling with it for a few days.

有什么帮助吗?

推荐答案

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

I have resolved a similar problem using a really simple subclass of UIScrollView of my own. You can take a look at it here - https://gist.github.com/ShadeApps/5a29e1cea3e1dc3df8c8. Works like charm for me.

这就是你初始化它的方式:

That's how you init it:

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

这篇关于UI滚动中心图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆