实施IOS 7放大,缩小过渡

时间:2014-08-24 10:41:12

标签: ios objective-c uiscrollview

我想在ios应用程序的主屏幕中实现放大缩小过渡,就像ios7 app启动器中的过渡一样。

我不确定我是否在正确的道路上,但我在主屏幕中创建了UIScrollView,并在滚动视图中创建了按钮。我将其中一个按钮的矩形框传递给zoomToRect,但没有进行缩放。

我的计划是转换到我的应用的另一个视图以及填充屏幕的按钮上的放大动画。

我在UIScrollView使用zoomToRect时遗漏了一些内容,或者在使用主屏幕的滚动视图时我完全错了。通过使用UIView作为我应用的主屏幕然后使用CGAffineTransformMakeScale转换进行缩放,我能够实现放大效果。

1 个答案:

答案 0 :(得分:0)

1.在你的stroryboard上将uiscrollView放在视图控制器上。 2.将uiimageView放在uiscrollView上 3.确保你的stroryboard没有自动布局。 4.在您的节目大小检查器上,确保scrollView和imageView的with和height是相同的。 5.确保单击自动调整大小的引脚。

转到分配给您正在处理的ViewController的UIViewContoller类。

在Header文件中添加以下属性:

         #import <UIKit/UIKit.h>

        @interface ViewController : UIViewController <UIScrollViewDelegate>


        @property (nonatomic, strong) UIImageView* imageViewPointer;
//Make sure scrollView is linked to the ui element on your storyboard
        @property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
//Make sure imageView is linked to the ui element on your storyboard
        @property (strong, nonatomic) IBOutlet UIImageView *imageView;

        // These properties are new
        @property (nonatomic, strong) NSMutableArray* imageViewConstraints;
        @property (nonatomic) BOOL imageViewConstraintsNeedUpdating;
        @property (nonatomic, strong) UIScrollView* scrollViewPointer;

    //Then in the implementation file this the following code to implement

    @interface ViewController ()

    @end

    @implementation ViewController

    @synthesize scrollView,imageView;

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
    {
        return self.imageViewPointer;
    }



    - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
    {
        if(_imageViewConstraintsNeedUpdating)
        {
            // Remove the previous image view constraints
            [self.view removeConstraints:_imageViewConstraints];

            // Replace them with new ones, this time constraining against the width & height of the scroll view's content, not the scroll view itself
            NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_scrollViewPointer, _imageViewPointer);
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_imageViewPointer(width)]|" options:0 metrics:@{@"width" : @(_scrollViewPointer.contentSize.width)} views:viewsDictionary]];
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_imageViewPointer(height)]|" options:0 metrics:@{@"height" : @(_scrollViewPointer.contentSize.height)} views:viewsDictionary]];

            self.imageViewConstraintsNeedUpdating = NO;
        }
    }



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


        [imageView setImage:[UIImage imageNamed:@"Your Image"]];

        imageView.contentMode = UIViewContentModeScaleAspectFit;

       scrollView.contentInset = UIEdgeInsetsZero;

        self.imageViewPointer = imageView;

        // New
        self.scrollViewPointer = scrollView;
        scrollView.maximumZoomScale = 5;
        scrollView.minimumZoomScale = 1.0;
        scrollView.delegate = self;

        NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(scrollView, imageView);
        NSLog(@"Current views dictionary: %@", viewsDictionary);
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];

        // Saving the image view width & height constraints
        self.imageViewConstraints = [[NSMutableArray alloc] init];
        // Constrain the image view to be the same width & height of the scroll view
        [_imageViewConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView(scrollView)]|" options:0 metrics: 0 views:viewsDictionary]];
        [_imageViewConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView(scrollView)]|" options:0 metrics: 0 views:viewsDictionary]];

        // Add the image view constraints to the VIEW, not the scroll view
        [self.view addConstraints:_imageViewConstraints];

        // Flag
        self.imageViewConstraintsNeedUpdating = YES;


    }

    @end