[英]UIView in UIScrollview. frame.origin not changing on scrolling, can't detect collision
我的屏幕上包含:
UIScrollView与UIView A重叠(直到大约一半),允许我“拖动” UIView B并将其与UIView A重叠。这样,当我放开它时,UIView B可以很好地弹回它原始位置(我不希望将其从屏幕的起点一直拖到UIView A上)
现在,我试图在UIView重叠时检测到碰撞。 但是,从未检测到。 我正在使用convertRect:toView:在同一系统上获取坐标,但是通过在touchesMoved中跟踪UIView B的坐标,我看到“拖动”(确实滚动UIScrollView)不会影响其框架的原点坐标。 它们的触动开始与每次触动时一样。
我假设这是因为UIViewB并没有真正移动到任何地方,UIScrollView是,这意味着UIView的原点始终是相同的,即使它的绝对坐标不是。 关于如何处理这个有什么建议吗?
当scrollView.frame.origin.x + viewB.frame.size.width + scrollView.contentOffset.x> = viewA.frame.origin.x时,会有重叠。
编辑后:
我无法使用滚动视图,因此我尝试了另一种效果很好的方法。 我没有使用滚动视图,而是使用了具有背景色的矩形视图,并在其中添加了蓝色正方形。 我在IB中为蓝色方块设置了一个设置的宽度和高度,将其在长皮包骨头视图中居中(在y方向上),并在该长视图的左侧设置了另一个约束。 IBOutlet leftCon连接到该约束。 然后,我使用以下代码将其拖动,并在放开时让其返回:
#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;
}
}
转换码中的数字(160和100)是通过记录凭经验确定的。 160号使它不偏离长视线的右边缘,而100号则是它开始与黑框重叠的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.