【问题标题】:Resize view right-to-left with animation on iOS在 iOS 上使用动画从右到左调整视图大小
【发布时间】:2012-08-15 18:26:08
【问题描述】:

我想将 UIView 从右侧调整到左侧(相反)。 现在我有这样的矩形视图:CGRectMake(100.f, 0.f, 100.f, 100.f);

[UIView animateWithDuration:0.5 animations:^{
  view.frame = CGRectMake(0.f, 0.f, 200.f, 100.f);
}];

问题是动画不好看。它向右弹跳(变大),然后移动到位置 0.0,0.0。

我想达到与从左到右调整大小时相同的效果。

编辑:代码

- (void)resizeSearchBar:(int)type
{
    //resize - bigger
    if (type == 1) {
        [UIView animateWithDuration:1.0 animations:^{
            self.searchBar.frame = CGRectMake(0.f, 0.f, 280.f, 44.f);
        }];
    }
    //resize - smaller
    else {
        [UIView animateWithDuration:1.0 animations:^{
            self.searchBar.frame = CGRectMake(95.f, 0.f, 185.f, 44.f);
        }];
    }
}

编辑 2

我也尝试过像这样更改 IB 中的视图属性:

仍然没有运气......

【问题讨论】:

  • 你能显示窗口创建代码吗?你是从哪个方法开始这个动画的?实际上,您似乎试图为错误的“属性”设置动画......
  • 其实我想调整 UISearchBar 的大小。它是从 nib 创建并连接到属性 self.mySearchBar

标签: iphone ios animation uiview


【解决方案1】:

得到这个答案的提示:Can't animate the frame or bounds of a UISearchBar

诀窍是在设置帧后在动画块中调用[searchBar layoutSubviews];

这是我在示例中的工作:

- (IBAction)resizeButtonTapped:(id)sender {
    //resize - bigger
    if (searchBar.frame.origin.x == 95) {
        [UIView animateWithDuration:1.0 animations:^{
            self.searchBar.frame = CGRectMake(0.f, 0.f, 280.f, 44.f);
            [searchBar layoutSubviews];
        }];
    }
    //resize - smaller
    else {
        [UIView animateWithDuration:1.0 animations:^{
            self.searchBar.frame = CGRectMake(95.f, 0.f, 185.f, 44.f);
            [searchBar layoutSubviews];
        }];
    }

}

【讨论】:

  • 这是我解决断断续续/弹出动画问题所需的线索——谢谢!但是,我没有调用layoutSubviews,而是调用了layoutIfNeeded,在与您完全相同的位置,然后动画按照需要运行。我注意到替换是因为,根据类 ref,您不应该直接调用 layoutSubviews
【解决方案2】:

它会反弹,因为持续时间要短得多!!

增加持续时间..我认为它会更清楚它的动画效果

K试试这个

[UIView animateWithDuration:1.0 
                  delay:0.0 
                options:UIViewAnimationCurveEaseInOut 
             animations:^ {

     self.searchBar.frame = CGRectMake(0.f, 0.f, 280.f, 44.f);
             } 
             completion:^(BOOL finished) {

             }];

Chk here for Options

【讨论】:

  • @BorutTomazin 试试这个 :) 希望对您有所帮助..更多了解块动画
【解决方案3】:

我认为这对您很有用,请看下面的演示链接...

https://github.com/ipup/PPRevealSideViewController?_tmc=UFw1P_Ai9brJd4WEpSlPbNrXBWOvsYZ6gNPi_derQik

我希望这对你有帮助.. :)

已编辑...

[UIView animateWithDuration:0.5
                          delay:4.0
                        options: UIViewAnimationOptionTransitionCrossDissolve
                     animations:nil
                     completion:^{
                         view.frame = CGRectMake(0.f, 0.f, 200.f, 100.f);}];

不确定,但试试这个并使用不同的动画,如“UIViewAnimationOptionTransitionCrossDissolve”等...... 希望对你有帮助....

【讨论】:

  • 这是一个很好的例子,但不适用于我的问题。我不仅要向左/向右移动视图,还要调整它的大小......
猜你喜欢
  • 1970-01-01
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 2012-01-30
相关资源
最近更新 更多