如何使用手势调整 ScrollView 中子视图的大小

发布于 2024-12-23 12:22:04 字数 95 浏览 1 评论 0原文

我想知道如何使用手势(特别是捏合手势)调整 ScrollView 中子视图的大小。我不想缩放(我的意思是,我不需要放大/缩小)其内容,但我需要调整视图框架的大小。谁能帮助我吗?

i'd like to know how to resize subviews within a ScrollView using gestures, specifically Pinch Gestures. I don't want to scale (I mean, I don't need to Zoom in/out) its content, but I need to resize the view's frame. Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2024-12-30 12:22:04

将捏合手势识别器添加到您的 mainView:

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handlePinchGesture:)];
pinchGesture.delegate = self;
[mainView addGestureRecognizer:pinchGesture];
[pinchGesture release];

然后对捏合做出反应:

- (IBAction)handlePinchGesture:(UIPinchGestureRecognizer *)sender {

    switch (sender.state) {
        case UIGestureRecognizerStateBegan:
            if([sender scale] < 1){
                NSArray *subViews = [mainView subviews];
                for( UIView *view in subViews){
                     //Adjust the size of your subview however you like
                     CGRect newFrame = CGRectMake(x,x,x,x);
                     view.frame = newFrame;
                }
            }else if([sender scale] > 1){ 
                //resize frame (zoom in)

            } 

            break;

        default:
            break;
    }
}

如果您的 mainView 有一个选项 @property(nonatomic) BOOL autoresizesSubviews 设置为 YES 您可以调整 mainView 的大小否则你将不得不遍历所有 mainView 子视图并相应地调整它们的大小

Add pinch gesture recognizer to your mainView:

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handlePinchGesture:)];
pinchGesture.delegate = self;
[mainView addGestureRecognizer:pinchGesture];
[pinchGesture release];

then react to the pinch:

- (IBAction)handlePinchGesture:(UIPinchGestureRecognizer *)sender {

    switch (sender.state) {
        case UIGestureRecognizerStateBegan:
            if([sender scale] < 1){
                NSArray *subViews = [mainView subviews];
                for( UIView *view in subViews){
                     //Adjust the size of your subview however you like
                     CGRect newFrame = CGRectMake(x,x,x,x);
                     view.frame = newFrame;
                }
            }else if([sender scale] > 1){ 
                //resize frame (zoom in)

            } 

            break;

        default:
            break;
    }
}

If your mainView has an option @property(nonatomic) BOOL autoresizesSubviews set to YES you could just resize the mainView frame otherwise you will have to go through all the mainView subviews and resize them accordingly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文