在objective-c中捏缩放后如何删除顶部和底部空间?

Posted

技术标签:

【中文标题】在objective-c中捏缩放后如何删除顶部和底部空间?【英文标题】:How to remove top and bottom space after pinch zoom in objective-c? 【发布时间】:2017-01-20 13:57:22 【问题描述】:

捏缩放后的问题,当我捏缩放和拖动图像后,顶部和底部有空白。

我的代码根据苹果文档。

https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html

示例代码

- (void)viewDidLoad 
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    scrollView.minimumZoomScale=0.5;
    scrollView.maximumZoomScale=6.0;
    scrollView.contentSize=CGSizeMake(1280, 960);
    scrollView.delegate = self;

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

    return imageView;

谢谢。

【问题讨论】:

你的imageView是如何放入ScrollView的? 是的图像视图放入滚动视图 【参考方案1】:

首先将最小缩放比例设置为 1.0:

    scrollView.minimumZoomScale = 1.0;

接下来,设置scrollView的contentSize:

    scrollView.contentSize = [UIScreen mainScreen].bounds.size;

如果您不想在弹跳时看到多余的空间,请添加下一行:

    scrollView.bounces = NO;

整个方法应该是这样的:

- (void)viewDidLoad

    [super viewDidLoad];

    self.scrollView.minimumZoomScale = 1.0;
    self.scrollView.maximumZoomScale = 6.0;

    self.scrollView.bounces     = NO;
    self.scrollView.contentSize = [UIScreen mainScreen].bounds.size;
    self.scrollView.delegate    = self;

希望它会有所帮助。

【讨论】:

【参考方案2】:

这就是我的做法。如果您的图像完全适合视图,则缩放和平移时不会有空白。

如果您的图片不适合或无法正确缩放,则侧面或顶部会有空白。

首先确保您的图片尺寸正确。接下来需要将minimumZoomScalemaximumZoomScale 添加到scrollView。其余的将在那之后工作。您可以使用自动布局或框架。由你决定。

此示例使用自动布局:

#import "ViewController.h"

@interface AutoLayoutScrollView : UIScrollView
@property (nonatomic, weak) UIView *contentView;
@end

@implementation AutoLayoutScrollView
- (instancetype)init 
    if (self = [super init]) 
        UIView *view = [[UIView alloc] init];
        self.contentView = view;

        [self addSubview:view];
        [self.contentView.leftAnchor constraintEqualToAnchor:self.leftAnchor].active = YES;
        [self.contentView.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;
        [self.contentView.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
        [self.contentView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES;
        [self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
    

    return self;


- (void)didMoveToSuperview 
    [super didMoveToSuperview];

    UIView *parent = self.superview;

    if (parent) 
        [self.leftAnchor constraintEqualToAnchor:parent.leftAnchor].active = YES;
        [self.rightAnchor constraintEqualToAnchor:parent.rightAnchor].active = YES;
        [self.topAnchor constraintEqualToAnchor:parent.topAnchor].active = YES;
        [self.bottomAnchor constraintEqualToAnchor:parent.bottomAnchor].active = YES;
        [self setTranslatesAutoresizingMaskIntoConstraints:NO];
    

@end


@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) AutoLayoutScrollView *scrollView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIPanGestureRecognizer *panGesture;
@end

@implementation ViewController

- (void)viewDidLoad 
    [super viewDidLoad];

    self.scrollView = [[AutoLayoutScrollView alloc] init];
    [self.view addSubview:self.scrollView];
    [self.scrollView.contentView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor].active = YES;
    [self.scrollView.contentView.heightAnchor constraintEqualToAnchor:self.view.heightAnchor].active = YES;

    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image"]];
    [self.imageView setContentMode:UIViewContentModeScaleAspectFit];

    [self.scrollView.contentView addSubview:self.imageView];
    [self.imageView.leftAnchor constraintEqualToAnchor:self.scrollView.contentView.leftAnchor].active = YES;
    [self.imageView.rightAnchor constraintEqualToAnchor:self.scrollView.contentView.rightAnchor].active = YES;
    [self.imageView.topAnchor constraintEqualToAnchor:self.scrollView.contentView.topAnchor].active = YES;
    [self.imageView.bottomAnchor constraintEqualToAnchor:self.scrollView.contentView.bottomAnchor].active = YES;
    [self.imageView setTranslatesAutoresizingMaskIntoConstraints:NO];

    [self.scrollView setMinimumZoomScale:1.0];
    [self.scrollView setMaximumZoomScale:3.0];
    [self.scrollView setZoomScale:1.0f animated:YES];
    [self.scrollView setDelegate:self];



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


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
    return self.imageView;

@end

【讨论】:

首先确保您的图像尺寸正确,我如何假设图像尺寸正确,因为图像由用户选择,滚动查看行为,我想要像 MWPhotoBrowser 我有附加链接github.com/mwaterfall/MWPhotoBrowser跨度>

以上是关于在objective-c中捏缩放后如何删除顶部和底部空间?的主要内容,如果未能解决你的问题,请参考以下文章

在 ios7 中的多个图像的 UIScrollview 中捏缩放不起作用

在滚动视图中捏放大

将图像视图图像置于顶部

如何删除屏幕底部和底栏之间的底部空间?

在 Scaffold Jetpack Compose 内的特定屏幕上隐藏顶部和底部导航器

如何在 UICollectionView 顶部添加一个视图以支持诸如搜索栏或 Objective-c 中的过滤器之类的内容?