gpt4 book ai didi

ios - Objective-C - 无法缩放嵌入到 UIScrollView 中的 UIImageView

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:03:32 25 4
gpt4 key购买 nike

我是这里的新人。很高兴认识大家。

我正在尝试实现 Ray Wenderlich 对此处描述的 UIScrollView 的缩放:

http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content

但是好像对我没用。我想做的是:

  • 从 nib 创建 UIImageView。
  • 将 UIImageView 添加到 UIScrollView。
  • 如果应用程序是从 iPad 执行的,则在 View 开始时以 2 的比例从图像中心放大。

这是我的代码:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

//Hide the status bar
[[UIApplication sharedApplication] setStatusBarHidden:YES];

//------------------------------
//Initialize the image view of the home screen
_mapView = (TM_MapView *)[[[NSBundle mainBundle]loadNibNamed:@"MapView" owner:self options:nil]objectAtIndex:0];

// set the content size to be the size our our whole frame
mapScrollView.contentSize = _mapView.imageView.image.size;
[mapScrollView setScrollEnabled:YES];

//set the selector for the buttons in the map view
[self setButtonsSelector];

// now add our scroll view to the main view
[mapScrollView addSubview:_mapView];
//------------------------------

NSLog(@"_mapView.imageView.frame: %@", NSStringFromCGRect( _mapView.imageView.frame));
NSLog(@"_mapView.imageView.image.size: %@", NSStringFromCGSize( _mapView.imageView.image.size));
NSLog(@"mapScrollView.contentSize: %@", NSStringFromCGSize( mapScrollView.contentSize));

}

-(void)viewWillAppear:(BOOL)animated
{
mapScrollView.minimumZoomScale = 1.0f;
mapScrollView.maximumZoomScale = 2.5f;
mapScrollView.zoomScale = 1.0f;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
[self zoomOnStart];
}
}

-(void)zoomOnStart
{
NSLog(@"Zooming on start");
CGPoint zoomPoint = CGPointMake(512.0f, 384.0f);

CGFloat newZoomScale = 2.0f;

CGSize scrollViewSize = self.mapScrollView.bounds.size;

CGFloat w = scrollViewSize.width / newZoomScale;
CGFloat h = scrollViewSize.height / newZoomScale;
CGFloat x = zoomPoint.x - (w/2.0f);
CGFloat y = zoomPoint.y - (h/2.0f);

CGRect rectToZoomTo = CGRectMake(x, y, w, h);
//CGRect rectToZoomTo = CGRectMake(300, 300, 200, 100);

NSLog(@"rectToZoomTo: %@", NSStringFromCGRect(rectToZoomTo));

[self.mapScrollView zoomToRect:rectToZoomTo animated:YES];

}

#pragma mark - Delegates
-(UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView
{
NSLog(@"Scroll View viewForZoomingInScrollView");
return _mapView;
}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
NSLog(@"Scroll View End Zooming!");
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
NSLog(@"Scroll View Zoom changed to: %f", scrollView.zoomScale);
}

如您所见,我记录了一些帧和委托(delegate)以查看缩放的输入并查看我们是否正在缩放。这是控制台日志:

2014-10-09 17:24:18.507 TrueMuzeiOS[13268:60b] _mapView.imageView.frame: {{0, 0}, {1024, 768}}
2014-10-09 17:24:18.511 TrueMuzeiOS[13268:60b] _mapView.imageView.image.size: {1024, 768}
2014-10-09 17:24:18.513 TrueMuzeiOS[13268:60b] mapScrollView.contentSize: {1024, 768}
2014-10-09 17:24:18.519 TrueMuzeiOS[13268:60b] Zooming on start
2014-10-09 17:24:18.521 TrueMuzeiOS[13268:60b] rectToZoomTo: {{320, 128}, {384, 512}}

如您所见,viewForZoomingInScrollView:scrollViewDidEndZooming:withView:atScale:scrollViewDidZoom: 中的日志未被调用,这意味着我们根本没有缩放(CMIIW)。我已经添加了 UIScrollViewDelegate,因此它应该可以正常工作。

那么,你们能帮帮我吗?恕我直言,我已经正确地执行了这些步骤。所以我现在迷路了。非常感谢。

最佳答案

是否设置了 UIScrollView 的 delegate 属性?

虽然您可以在 Interface Builder 上完成,但我在您发布的代码中没有看到这一点。

如果是这种情况,只需这样就可以调用您的方法:

mapScrollView.delegate = self;

查看有关委托(delegate)属性部分的 UIScrollView 文档以了解更多信息。

https://developer.apple.com/library/ios/documentation/uikit/reference/uiscrollview_class/index.html#//apple_ref/occ/instp/UIScrollView/delegate

关于ios - Objective-C - 无法缩放嵌入到 UIScrollView 中的 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26276433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com