0

この質問を数回編集しましたが、まだ画像を 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

全体的な問題は、自分の写真を正しく表示できず、その周りにスペースがなく、それを拡大することができず、ロード時に正しい寸法である必要があることです。私は数日間それに苦労してきました。

何か助けはありますか?

4

2 に答える 2

1

Peter Steinberger によるこの問題に関する素晴らしいブログ投稿があります: http://petersteinberger.com/blog/2013/how-to-center-uiscrollview/

于 2013-08-19T06:01:10.000 に答える