【问题标题】:modify the width of an imageView with animation用动画修改imageView的宽度
【发布时间】:2011-08-10 22:31:08
【问题描述】:

我有一个图像“image1”,它是一个 UIButton,我想要的是,如果我触摸这个按钮,图像的宽度将乘以 2(带有动画),而不是高度只是宽度。 对不起我的英语我是法国人:/

【问题讨论】:

    标签: iphone xcode animation uiimageview width


    【解决方案1】:

    这对于块动画来说相当容易。只需尝试:

    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    [UIView animateWithDuration:1.0
                           animations:^{ 
                             CGRect newRect = imageView.frame;
                             int movementToTheLeft = imageView.frame.size.width / 2;
                             newRect.size.width *= 2;
                             newRect.origin.x -= movementToTheLeft;
                             imageView.frame = newRect;
                           }
                           completion:^(BOOL finished){
    
                             [UIView animateWithDuration:1.0
                                              animations:^{ 
                                                NSLog(@"Scaled!");// Anything can go in  this completion block, it should be used for any logic you need after the animation.
                                              } 
                                              completion:^(BOOL finished){
                                                ;
                                              }];
    

    这应该将您的 imageView 的宽度缩放 2。希望对您有所帮助!

    【讨论】:

    • 除了完成块中的第二个 animateWithDuration 之外的好答案。你真的是那个意思吗?
    • 我通常用它来设置一个布尔变量,它会阻止用户按下按钮,直到动画完成。也可以为您的代码触发其他动画或通知。你真的不需要它,这只是我的偏好。是的,对于这个例子来说,1.0 有点太大了!
    • 这段代码不起作用,因为当我触摸按钮时,我的按钮向右移动并且它的宽度没有改变,它只是向右移动
    • 你正在移动一个 imageView 对吗?将运动也放入动画中,这应该会处理它。
    • 另外,你必须确保图像改变大小以适应 imageView。尝试使用不同的内容模式选项来实现这一点。
    【解决方案2】:

    老式的方式(iOS 4.0 之后不推荐,但仍然有效):

    [UIView beginAnimations:@"widen" context:nil];
    [UIView setAnimationDuration:1.0];
    CGRect newRect = imageView.frame;
    newRect.width *= 2;
    imageView.frame = newRect;
     // ... set more parameters if desired
    [UIView commitAnimations];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 2014-02-28
      • 2021-02-01
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      相关资源
      最近更新 更多