首页 文章

在UIScrollView问题中旋转UIImageView

提问于
浏览
2

我必须像照片应用程序一样实现旋转工作 . 将 UIImageView 放在 UIScrollView 中 . 当设备旋转时,我希望图像像Photo一样旋转到风景 . 如果图像是横向图像,则它将使用旋转动画填充整个 UIScrollView 的宽度 . 当下面的代码

[UIView animateWithDuration:0.36
               animations:^(void){
                [_imageView setTransform:CGAffineTransformMakeRotation(M_PI*90/180)];
                 _imageView.frame = CGRectMake(0, 0, 480, 320); //set the frame after the rotate
                self.contentSize = CGSizeMake(480, 320); //set the content-size of the scrollview
               }
               completion:^(BOOL complete){
               }];

但这不起作用 . 旋转发生,但 _imageView 的位置不正确,或图像没有填充宽度 . 即使我在变换之前更改了顺序框架和内容大小,仍然不正确 .

有这个的正确方法是什么?

3 回答

  • 0

    WWDC 2010 Video: Designing Apps with Scrolls Views向您展示了如何做到这一点 .

  • 1

    在我看来,正确的答案是不要自己动画轮换 .

    如果您按照最新的 iOS 6 指南工作,那么您只需在 ViewController layoutSubviews() 方法中重新排列 views .

  • 1

    @CoDEFRo引用的源代码类似于以下内容,但在这里我将它放入UIScrollView的委托方法scrollViewDidScroll: .

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {   
        CGSize boundsSize = scrollView.bounds.size;
        CGRect frameToCenter = self.imageView.frame;
    
        if (frameToCenter.size.width < boundsSize.width)
            frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
        else
            frameToCenter.origin.x = 0;
    
        if (frameToCenter.size.height < boundsSize.height)
            frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
        else
            frameToCenter.origin.y = 0;
    
        self.imageView.frame = frameToCenter;
    }
    

相关问题