简体   繁体   English

UIScrollview中的UIView。 frame.origin在滚动时未更改,无法检测到碰撞

[英]UIView in UIScrollview. frame.origin not changing on scrolling, can't detect collision

I have a screen containing: 我的屏幕上包含:

  • UIView A UIView A
  • UIScrollView, which contains UIView B UIScrollView,其中包含UIView B

The UIScrollView overlaps with UIView A (up until about halfway through it), allowing me to "drag" UIView B and make it overlap it with UIView A. This is so that when I let go of it, UIView B bounces nicely back into its original position (I don't want to be able to drag it all around the screen, just from its starting point onto UIView A) UIScrollView与UIView A重叠(直到大约一半),允许我“拖动” UIView B并将其与UIView A重叠。这样,当我放开它时,UIView B可以很好地弹回它原始位置(我不希望将其从屏幕的起点一直拖到UIView A上)

Now, I'm trying to detect a collision when the UIViews overlap. 现在,我试图在UIView重叠时检测到碰撞。 However, none is ever detected. 但是,从未检测到。 I am using convertRect:toView: to get coordinates on the same system, but from tracing UIView B's coordinates in its touchesMoved I see that "dragging" (scrolling the UIScrollView, really) doesn't affect its frame's origin coordinates. 我正在使用convertRect:toView:在同一系统上获取坐标,但是通过在touchesMoved中跟踪UIView B的坐标,我看到“拖动”(确实滚动UIScrollView)不会影响其框架的原点坐标。 They are the same in touchedBegan as they are every time touchedMoved fires. 它们的触动开始与每次触动时一样。

I'm assuming this is because UIView B isn't really moving anywhere, UIScrollView is, meaning that the UIView's origin point is always the same, even if its absolute coordinates aren't. 我假设这是因为UIViewB并没有真正移动到任何地方,UIScrollView是,这意味着UIView的原点始终是相同的,即使它的绝对坐标不是。 Any suggestion on how to handle this? 关于如何处理这个有什么建议吗?

There will be overlap when scrollView.frame.origin.x + viewB.frame.size.width + scrollView.contentOffset.x >= viewA.frame.origin.x 当scrollView.frame.origin.x + viewB.frame.size.width + scrollView.contentOffset.x> = viewA.frame.origin.x时,会有重叠。

After Edit: 编辑后:

I couldn't get the scroll view to work so I tried it another way that worked pretty well. 我无法使用滚动视图,因此我尝试了另一种效果很好的方法。 Instead of a scroll view, I just used a rectangular view with a background color, and added the blue square to it. 我没有使用滚动视图,而是使用了具有背景色的矩形视图,并在其中添加了蓝色正方形。 I gave the blue square a set width and height in IB, centered it in the long skinny view (in the y direction), and had one other constraint to the left side of that long view. 我在IB中为蓝色方块设置了一个设置的宽度和高度,将其在长皮包骨头视图中居中(在y方向上),并在该长视图的左侧设置了另一个约束。 The IBOutlet leftCon is connected to that constraint. IBOutlet leftCon连接到该约束。 I then used this code to drag it, and have it go back when I let go: 然后,我使用以下代码将其拖动,并在放开时让其返回:

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()
@property (strong,nonatomic) UIPanGestureRecognizer *panner;
@property (strong,nonatomic) IBOutlet NSLayoutConstraint *leftCon;
@property (strong,nonatomic) CADisplayLink *displayLink;
@end

@implementation ViewController {
    IBOutlet UIView *blueSquare;
}

-(void)viewDidLoad {
    self.panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    [blueSquare addGestureRecognizer:self.panner];
}

- (void)handlePanGesture:(UIPanGestureRecognizer *)sender {
    CGPoint translate = [sender translationInView:self.view];
    if (self.leftCon.constant <160)
        if (self.leftCon.constant > 100) NSLog(@"Bang! We have a collision");
        self.leftCon.constant = translate.x;
    if (sender.state == UIGestureRecognizerStateEnded){
        [self startDisplayLink];
    }
}


-(void)startDisplayLink {
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(bounceBack:)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)stopDisplayLink {
    [self.displayLink invalidate];
    self.displayLink = nil;
}


-(void)bounceBack:(CADisplayLink *) link {
    self.leftCon.constant -= 12;
    if (self.leftCon.constant < 2) {
        [self stopDisplayLink];
        self.leftCon.constant = 2;
    }
}

The numbers in the translate code (160 and 100) were determined empirically by logging. 转换码中的数字(160和100)是通过记录凭经验确定的。 The 160 number keeps it from going off the right edge of the long view, and the 100 number is where it starts to overlap the black box. 160号使它不偏离长视线的右边缘,而100号则是它开始与黑框重叠的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM