iOS:使用按钮缩放图像

时间:2013-05-07 21:17:49

标签: ios xcode zoom scrollview

我有一个imageView作为应用程序的背景。我在背景上有一个按钮。我想按下那个按钮并放大背景图像的动画。解决这个问题的最佳方法是什么?我应该使用scrollview吗?

3 个答案:

答案 0 :(得分:3)

我认为在动画块中使用变换是最好的方法。 你可以这样做:

[UIView animateWithDuration:0.3 animations:^
{
    [yourImageView setTransform:CGAffineTransformMakeScale(1.5, 1.5)];
}];

并将其放回初始位置,您只需要执行此操作:

[UIView animateWithDuration:0.3 animations:^
{
    [yourImageView setTransform:CGAffineTransformIdentity];
}];

答案 1 :(得分:2)

这样的东西?

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(80, 50, 150, 40);
[myButton setTitle:@"Your button!" forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:@"xyz.png"]];     
[self.view addSubview:myButton];

-(IBAction)buttonClicked:(UIButton *)sender
{
   self.frame = CGRectMake(0.0f, 0.0f, 200.0f, 150.0f);
   [UIView beginAnimations:@"Zoom" context:NULL];
   [UIView setAnimationDuration:0.5];
   self.frame = CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f);
   [UIView commitAnimations];
}

答案 2 :(得分:0)

swift 4

    func zoomOut(){
    DispatchQueue.main.async{[weak self] in
        UIView.animate(withDuration: 0.3, animations: {[weak self] in
            self?.imageView.transform = .identity
        })
    }
}

func zoomIn(){
    DispatchQueue.main.async{[weak self] in
        UIView.animate(withDuration: 0.3, animations: {[weak self] in
            self?.imageView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
        })
    }
}