开发者

How do I center a specific ImageView-coordinate in a UIScrollView

How do I best position an image (UIImageView) within a UIScrollView programmatically?

I have the x and y coordinates (CGPoint C = centerCoordinate) of the image position that needs to be centered horizontally and vertically in the UIScrollView.

===Image=============================================
|                                                   |
|                                                   |
|              ==ScrollView===                      |
|              |             |                      |
|              |      C      |                      |
|              |             |           开发者_高级运维           |
|              ===============                      |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
=====================================================

I tried a) working with the center property of the ImageView, but don't seem to make progress here. b) then I tried calculating the frame-property by setting the origin to

  C.x = -1 * centerCoordinate.x + ScrollView.frame.size.width / 2
  C.y = -1 * centerCoordinate.y + ScrollView.frame.size.width / 2

but don't succeed.

any hints?

regards


I think I found the way to do this. What threw me is how to take account of the zoomScale as well as factoring in the right frame coordinates since these change during zoom & scroll activities.

The solution that seems to work now is implemented in the UIScrollView and looks like this:

- (void) setViewPosition:(CGPoint) tapPoint zoomScale:(NSNumber *)zoomScale {
   ...
   self.zoomScale = zoomScale.floatValue;

   // Determine new center coordinate
   CGPoint mCOffset;
   mCOffset.x = tapPoint.x * self.zoomScale - frameSizeIvar.width  / 2;
   mCOffset.y = tapPoint.y * self.zoomScale - frameSizeIvar.height / 2;

   // Check whether center coordinate will place the map out of range
   // x-coordinate
   if ( mCOffset.x < 0 ) 
       mCOffset.x = 0;
   else if ( mCOffset.x > imageView.bounds.size.width * self.zoomScale - frameSizeIvar.width)
       mCOffset.x = imageView.bounds.size.width * self.zoomScale - frameSizeIvar.width;

   // y-coordinate
   if ( mCOffset.y < 0 ) 
      mCOffset.y = 0;
   else if ( mCOffset.y > iMapImageView.bounds.size.height * self.zoomScale - frameSizeIvar.height)
      mCOffset.y = iMapImageView.bounds.size.height * self.zoomScale - frameSizeIvar.height;

   self.contentOffset = mCOffset;
   ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜