将自定义 UIView 添加到 UIScrollview - 如何计算位置、大小和处理缩放? [英] Adding custom UIViews to a UIScrollview - how to calculate position, size and handle zoom?

查看:29
本文介绍了将自定义 UIView 添加到 UIScrollview - 如何计算位置、大小和处理缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个 iOS 新手,我正在尝试开发 观察此通知并通过以下代码在 UIScrollView 中添加/删除磁贴:

- (void) handleTileMoved:(NSNotification*)notification {Tile* tile = (Tile*)notification.object;如果 (tile.superview != _scrollView &&CGRectIntersectsRect(tile.frame, _scrollView.frame)) {[tile removeFromSuperview];[_scrollView addSubview:tile];tile.frame = CGRectMake(tile.frame.origin.x + _scrollView.contentOffset.x,tile.frame.origin.y + _scrollView.contentOffset.y,kTileWidth * _scrollView.zoomScale,kTileScale * _scrollView.zoomScale);} else if (tile.superview == _scrollView &&!CGRectIntersectsRect(tile.frame, _scrollView.frame)) {[tile removeFromSuperview];[self.view addSubview:tile];[自调整帧数];}}

这种工作,但我有两个问题 -

问题 1:如何计算字母磁贴的位置和大小,当我将它添加到滚动视图时?

iOS 是否为此提供了任何舒适的功能,还是我必须手动计算这些值(如上面的代码所示)?

问题 2: 当我缩放图像视图(游戏板)时,放置在其上的字母不会缩放.可能是因为这个方法?

- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView{返回_imageView;}

如何解决这个问题?上面的方法不能返回多个对象

更新:

我已经):

解决方案

问题 2:使用新的 contentView 作为板子和棋子的超级视图.这样,棋盘坐标中棋子的位置和大小将独立于应用于 contentView 的缩放和平移.

问题 1:不再有问题 1!

As an iOS newbie I'm trying to develop a (yet another) word game.

I have a UIScrollView holding an UIImageView with the board image.

The UIScrollView can be zoomed by double tap and pinch gestures:

Underneath the UIScrollView I have 7 draggable UIViews representing letter tiles.

The implementation in Tile.m handles dragging with touchesBegan, touchesMoved and posts a notification in touchesEnded.

The single view controller ViewController.m observes this notification and adds/removes the tile to/from the UIScrollView by the following code:

- (void) handleTileMoved:(NSNotification*)notification {
    Tile* tile = (Tile*)notification.object;

    if (tile.superview != _scrollView && 
        CGRectIntersectsRect(tile.frame, _scrollView.frame)) {

        [tile removeFromSuperview];
        [_scrollView addSubview:tile];
        tile.frame = CGRectMake(tile.frame.origin.x + _scrollView.contentOffset.x,
                                tile.frame.origin.y + _scrollView.contentOffset.y,
                                kTileWidth * _scrollView.zoomScale,
                                kTileScale * _scrollView.zoomScale);

    } else if (tile.superview == _scrollView &&
               !CGRectIntersectsRect(tile.frame, _scrollView.frame)) {

        [tile removeFromSuperview];
        [self.view addSubview:tile];
        [self adjustFrames];
    }
}

This kind of works, but I have 2 problems -

Problem 1: How to calculate the position and size of the letter tile, when I am adding it to the scroll view?

Are there any comfortable functions provided by iOS for that purpose or do I have to calculate those values manually (as shown in my code above)?

Problem 2: When I zoom the image view (the game board), then the letter pieces placed on it don't zoom. Probably because of this method?

- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView
{
    return _imageView;
}

How to fix this problem? I can't return several objects by the above method

UPDATE:

I have branched my app on GitHub and inserted a contentView between the scrollView and imageView (here fullscreen):

Zooming the image and the tiles works then, but I can't scroll anymore.

I've printed out the contentSize and it seem to be okay with 1000x1000:

-[ViewController viewDidLoad]: image {1000, 1000}
-[ViewController viewDidLayoutSubviews] contentOffset={0, 0} contentSize={1000, 1000}
tile: N 9 {2, 433} {45, 45}
tile: V 1 {47, 433} {45, 45}
tile: L 9 {92, 433} {45, 45}
tile: R 4 {137, 433} {45, 45}
tile: B 7 {182, 433} {45, 45}
tile: B 8 {227, 433} {45, 45}
tile: M 5 {272, 433} {45, 45}
-[ViewController adjustZoom]: scrollView origin={0, 0} size={320, 431} minScale=0.320000 zoomScale=0.640000 maxScale=0.640000
-[ViewController adjustZoom]: imageView origin={0, 0} size={1000, 1000}
-[ViewController adjustZoom]: contentView origin={0, 0} size={640, 640}

I've tried changing _scrollView.canCancelContentTouches and _scrollView.translatesAutoresizingMaskIntoConstraints.

Also I've tried changing the interactivity checkbox of the UIImageView in Interface Builder, but it hasn't helped.

I couldn't figure out the problem by using Reveal app either (here fullscreen):

解决方案

Problem 2: Use your new contentView as superview for the board and the pieces. That way the position and the size of the pieces in the board coordinates will be independent of the zoom and pan applied to the contentView.

Problem 1: There is no longer problem 1!

这篇关于将自定义 UIView 添加到 UIScrollview - 如何计算位置、大小和处理缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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