Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There will be overlap when scrollView.frame.origin.x + viewB.frame.size.width + scrollView.contentOffset.x >= viewA.frame.origin.x</p> <p><strong>After Edit:</strong></p> <p>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. The IBOutlet leftCon is connected to that constraint. I then used this code to drag it, and have it go back when I let go:</p> <pre><code>#import "ViewController.h" #import &lt;QuartzCore/QuartzCore.h&gt; @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 &lt;160) if (self.leftCon.constant &gt; 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 &lt; 2) { [self stopDisplayLink]; self.leftCon.constant = 2; } } </code></pre> <p>The numbers in the translate code (160 and 100) were determined empirically by logging. 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload