使用缩放将UIScrollView滚动到某个位置(Scroll UIScrollView to a certain position with zooming)

编程入门 行业动态 更新时间:2024-10-26 06:30:13
使用缩放将UIScrollView滚动到某个位置(Scroll UIScrollView to a certain position with zooming)

我在UIScrollView中有一个UIImageView,在scrollView中启用了缩放。 我想要的是如果用户放大了图像的特定区域,然后返回,滚动视图将图像设置在任何地方。 我尝试过使用内容偏移,但它并不适用于所有缩放级别。 在缩放级别1的情况下,imageView类型粘在scrollView的左侧。 这是我用来保存contentOffset和zoomScale的代码。

-(void)saveContentOffset { NSInteger zoomValue=[Scroll zoomScale]; CGPoint offeset=[Scroll contentOffset]; NSMutableDictionary *dict=[[NSMutableDictionary alloc] init]; [dict setObject:[NSNumber numberWithInteger:offeset.x] forKey:@"X"]; [dict setObject:[NSNumber numberWithInteger:offeset.y] forKey:@"Y"]; [dict setObject:[NSNumber numberWithInteger:zoomValue] forKey:@"Zoom"]; [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"Dict"]; }

此代码用于在用户返回屏幕时设置contentOffset:

-(void)setContentOffsetForScroll { NSDictionary *dict=[[NSUserDefaults standardUserDefaults] objectForKey:@"Dict"]; if ([dict allKeys]>0) { NSInteger xPoint=[[dict valueForKey:@"X"] integerValue]; NSInteger yPoint=[[dict valueForKey:@"Y"] integerValue]; NSInteger zoomScale=[[dict valueForKey:@"Zoom"] integerValue]; CGPoint offsetPoint=CGPointMake(xPoint, yPoint); [Scroll setZoomScale:zoomScale]; [Scroll setContentOffset:offsetPoint]; } }

如果是zoomLevel 1,请查看附图中的问题,使用greenColor的背景视图是ScrollView。 在这种情况下,内容设置为左。

请告诉我到底缺少什么。

谢谢!

I have a UIImageView inside a UIScrollView with zooming enabled in the scrollView. What I want is if a user has zoomed into a particular region of an image , and then comes back , the scroll view sets the image wherever it was left. I have tried using content offset but it's not working for all zoom levels. In the case of zoom level 1 the imageView kind of sticks to the left of the scrollView . This is the code that I have used to save the contentOffset and zoomScale .

-(void)saveContentOffset { NSInteger zoomValue=[Scroll zoomScale]; CGPoint offeset=[Scroll contentOffset]; NSMutableDictionary *dict=[[NSMutableDictionary alloc] init]; [dict setObject:[NSNumber numberWithInteger:offeset.x] forKey:@"X"]; [dict setObject:[NSNumber numberWithInteger:offeset.y] forKey:@"Y"]; [dict setObject:[NSNumber numberWithInteger:zoomValue] forKey:@"Zoom"]; [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"Dict"]; }

and this code is used to set the contentOffset when the user comes back on the screen:

-(void)setContentOffsetForScroll { NSDictionary *dict=[[NSUserDefaults standardUserDefaults] objectForKey:@"Dict"]; if ([dict allKeys]>0) { NSInteger xPoint=[[dict valueForKey:@"X"] integerValue]; NSInteger yPoint=[[dict valueForKey:@"Y"] integerValue]; NSInteger zoomScale=[[dict valueForKey:@"Zoom"] integerValue]; CGPoint offsetPoint=CGPointMake(xPoint, yPoint); [Scroll setZoomScale:zoomScale]; [Scroll setContentOffset:offsetPoint]; } }

Please check the attached image for the issue in the case of zoomLevel 1 , the view at background with greenColor is ScrollView . In this case the content sets to left .

Please suggest what exactly I am missing.

Thanks!

最满意答案

你可以用它

通过x = imageview.frame.origin.x和y = imageview.frame.origin.y保存图像视图的原点

然后当你回到同一个视图时

使用此CGRect * rect = imageview.frame; rect.origin.x = x; //来自先前存储的值rect.origin.y = y; //来自先前存储的值imageview.frame = rect;

after a lot of research , i found the error that i was doing. I was using integer value for zoom scale , but to store precision data and exact value we need to use float value. So the changes are as below:

-(void)saveContentOffset { CGFloat zoomValue=[Scroll zoomScale]; CGPoint offeset=[Scroll contentOffset]; NSMutableDictionary *dict=[[NSMutableDictionary alloc] init]; [dict setObject:[NSNumber numberWithInteger:offeset.x] forKey:@"X"]; [dict setObject:[NSNumber numberWithInteger:offeset.y] forKey:@"Y"]; [dict setObject:[NSNumber numberWithfloat:zoomValue] forKey:@"Zoom"]; [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"Dict"]; } -(void)setContentOffsetForScroll { NSDictionary *dict=[[NSUserDefaults standardUserDefaults] objectForKey:@"Dict"]; if ([dict allKeys]>0) { NSInteger xPoint=[[dict valueForKey:@"X"] integerValue]; NSInteger yPoint=[[dict valueForKey:@"Y"] integerValue]; NSInteger zoomScale=[[dict valueForKey:@"Zoom"] floatValue]; CGPoint offsetPoint=CGPointMake(xPoint, yPoint); [Scroll setZoomScale:zoomScale]; [Scroll setContentOffset:offsetPoint]; } }

更多推荐

本文发布于:2023-07-29 14:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316737.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缩放   位置   UIScrollView   zooming   Scroll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!