我在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]; } }更多推荐
发布评论