JVC
JVC

Reputation: 570

How to synchronize UIScrollView image with another UIImageView

I'm designing a game that has a large hex style map in a UIScrollView and a 1/10th scale "mini map" in another view. The mini map has a rectile that is used to indicate the current area visible in the scroll view. What's the best way to position the rectile to synchronize it with the main map?

Upvotes: 1

Views: 311

Answers (1)

Jason Grandelli
Jason Grandelli

Reputation: 342

You can get your UIScrollView's contentOffset whenever you'd like. You could check this value in the UIScrollView delegate method - (void)scrollViewDidScroll:(UIScrollView *)scrollView. Once you have the contentOffset you should divide that by your UIScrollView's contentSize minus it's frame.size. That gives you a float, that float can then be used to determine the positioning of the rectile in the smaller view. Some code below to show what I'm talking about. I'm just going to give you the x positioning example. rinse and repeat for y positioning.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat xRat;
    CGFloat xPos;

    xRat = scrollView.contentOffset.x/(scrollView.contentSize.width - scrollView.frame.size.width);
}

The xRat is the percentage (in float format) that the scroll view has scrolled. So just use xRat to position the rectile along the x-axis by that percentage.

Upvotes: 2

Related Questions