Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Brad Larson pointed me down the right road with his suggestion to put the <code>UIImageView</code> inside a <code>UIScrollView</code>.</p> <p>In the end I put the <code>UIImageView</code> inside of a <code>UIScrollView</code>, and set the scrollView's <code>contentSize</code> <strong>and</strong> the <code>imageView</code>'s bounds to be the same size as the image in the UIImage:</p> <pre><code>UIImage* image = imageView.image; imageView.bounds = CGRectMake(0, 0, image.size.width, image.size.height); scrollView.contentSize = image.size; </code></pre> <p>Then, I can animate the scrollView's <code>contentOffset</code> to achieve a nice panning effect:</p> <pre><code>[UIView beginAnimations:@"pan" context:nil]; [UIView setAnimationDuration:animationDuration]; scrollView.contentOffset = newRect.origin; [UIView commitAnimations]; </code></pre> <p>In my particular case, I'm panning to a random space in the image. In order to find a proper rect to pan <strong>to</strong> and a proper duration to get a nice constant speed, I use the following:</p> <pre><code>UIImage* image = imageView.image; float xNewOrigin = [TCBRandom randomIntLessThan:image.size.width - scrollView.bounds.size.width]; float yNewOrigin = [TCBRandom randomIntLessThan:image.size.height - scrollView.bounds.size.height]; CGRect oldRect = scrollView.bounds; CGRect newRect = CGRectMake( xNewOrigin, yNewOrigin, scrollView.bounds.size.width, scrollView.bounds.size.height); float xDistance = fabs(xNewOrigin - oldRect.origin.x); float yDistance = fabs(yNewOrigin - oldRect.origin.y); float hDistance = sqrtf(powf(xDistance, 2) + powf(yDistance, 2)); float hDistanceInPixels = hDistance; float animationDuration = hDistanceInPixels / speedInPixelsPerSecond; </code></pre> <p>I'm using a <code>speedInPixelsPerSecond</code> of <code>10.0f</code>, but other applications might want to use a different value.</p>
 

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