【发布时间】:2014-02-19 11:29:46
【问题描述】:
在 Xcode 5.0.2 中,我创建了 a universal app 并将 UIScrollView 和 UIImageView(其“查看模式”更改为“左上”)拖到 iPhone 和 iPad 故事板中(此处为 fullscreen ):
为了使 UIScrollView 填满整个屏幕,我添加了 4 个约束(此处为 fullscreen):
然后在ViewController.m我尝试:
- 加载
board.png(版权所有 Wikipedia Commons,Denelson83) - 给
UIImageView提供与board.png相同的维度 - 将
UIScrollView的contentSize设置为相同大小
这里是源代码:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *img = [UIImage imageNamed:@"board"];
NSLog(@"%s: (%f x %f)", __PRETTY_FUNCTION__, img.size.width, img.size.height);
_imageView.image = img;
_imageView.frame = CGRectMake(0, 0, img.size.width, img.size.height);
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
_scrollView.contentSize = _imageView.bounds.size;
}
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
// fruitless attempt to solve the rotation problem
_scrollView.contentSize = _imageView.bounds.size;
}
这里是输出:
Scroll[3464:70b] -[ViewController viewDidLoad]: (1000.000000 x 961.500000)
这种方法可行,但有两个问题:
问题一:
当我旋转 iPhone 或 iPad 模拟器时(通过按 Cmd - 左箭头键)- 图像滚动停止工作:
问题 2:
在纵向模式下滚动有效,但我无法到达右下角:
更新 1:
我尝试了 iphonic 的建议(谢谢!),我看到 deviceOrientationDidChange 选择器被调用,但应用程序仍然不滚动:
Scroll[702:70b] -[ViewController viewDidLoad]: image size = {1000, 961.5}
Scroll[702:70b] -[ViewController deviceOrientationDidChange:]: view size = {1000, 961.5}
Scroll[702:70b] -[ViewController deviceOrientationDidChange:]: view size = {1000, 961.5}
Scroll[702:70b] -[ViewController deviceOrientationDidChange:]: view size = {1000, 961.5}
(不过我使用的是_imageView.image.size,因为_imageView.size 在我的sotryboards 中被定义为100 x 100):
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *img = [UIImage imageNamed:@"board"];
NSLog(@"%s: image size = %@", __PRETTY_FUNCTION__, NSStringFromCGSize(img.size));
_imageView.image = img;
_imageView.frame = CGRectMake(0, 0, img.size.width, img.size.height);
[[NSNotificationCenter defaultCenter]
addObserver:self
selector: @selector(deviceOrientationDidChange:)
name: UIDeviceOrientationDidChangeNotification
object: nil];
}
-(void)deviceOrientationDidChange:(NSNotification *)notification
{
_scrollView.contentSize = _imageView.image.size;
NSLog(@"%s: view size = %@", __PRETTY_FUNCTION__, NSStringFromCGSize(_imageView.image.size));
}
【问题讨论】:
-
好像
ContentSize有问题
标签: ios iphone objective-c uiscrollview rotation