【问题标题】:Making zoom-in & out view with UIScrollView使用 UIScrollView 进行放大和缩小视图
【发布时间】:2014-03-24 02:58:08
【问题描述】:

我正在尝试使用 UIScrollView 制作一些放大和缩小视图。 这就像第一次点击使视图放大 3 倍,再点击一次使其再次放大 3 倍,然后再点击将其缩小回原始大小。 但是我的代码不能正常工作。 这有什么问题? 谢谢,

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UIScrollView *myScrollView;
@property (strong, nonatomic) IBOutlet UIImageView *myImageView;


@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.myScrollView.delegate = self;

    self.myScrollView.minimumZoomScale = 1;
    self.myScrollView.maximumZoomScale = 8;

    self.myScrollView.scrollEnabled = YES;
    self.myScrollView.showsHorizontalScrollIndicator = YES;
    self.myScrollView.showsHorizontalScrollIndicator = YES;



    UITapGestureRecognizer  *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];

    doubleTapGesture.numberOfTapsRequired = 2;
    self.myImageView.userInteractionEnabled = YES;
    [self.myImageView addGestureRecognizer:doubleTapGesture];




}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)doubleTap:(UITapGestureRecognizer *)gesture
{

    if (self.myScrollView.zoomScale < self.myScrollView.maximumZoomScale){

        float newScale = self.myScrollView.zoomScale * 3;
        CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gesture locationInView:gesture.view]];

        [self.myScrollView zoomToRect:zoomRect animated:YES];

    }else{

        [self.myScrollView setZoomScale:1.0 animated:YES];
    }

}

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
{
    CGRect zoomRect;
    zoomRect.size.height = [self.myScrollView frame].size.height * scale;
    zoomRect.size.width =  [self.myScrollView frame].size.width  * scale;

    zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
    zoomRect.origin.y = center.y - (zoomRect.size.width / 2.0);

    return zoomRect;


}





#pragma mark - delegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.myImageView;
}



@end

【问题讨论】:

    标签: objective-c uiscrollview zooming


    【解决方案1】:
    - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
    {
        CGRect zoomRect;
        zoomRect.size.height = [self.myScrollView frame].size.height / scale;
        zoomRect.size.width =  [self.myScrollView frame].size.width  / scale;
    
        zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
        zoomRect.origin.y = center.y - (zoomRect.size.width / 2.0);
    
        return zoomRect;
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-09
      • 1970-01-01
      • 2016-08-11
      • 2010-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多