将视图扩展到左侧,动画

时间:2010-06-15 15:11:07

标签: objective-c iphone core-animation quartz-graphics

我有一个视图,我想使用动画在左侧扩展。所有边框但左边的边框应保持不变,因此视图的x位置和宽度都在变化。

我使用此代码:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.0];
self.frame = CGRectMake(self.frame.origin.x-100,
                        self.frame.origin.y,
                        self.frame.size.width+100,
                        self.frame.size.height);
[UIView commitAnimations];

如果我运行此代码,视图的宽度会立即设置为新值,然后视图将移动到新的x点,但为什么呢?我该如何改变这种行为?

感谢您的想法!

2 个答案:

答案 0 :(得分:0)

您的代码运行正常。你是怎么跑的?调用-setFrame是您正在做的事情的正确选择。以下是我的自定义视图:

#import "TestView.h"

@implementation TestView

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

- (void)go;
{
  [UIView beginAnimations:nil context:nil];
  [UIView setAnimationDuration:5.0];
  self.frame = CGRectMake(self.frame.origin.x-100,
                                 self.frame.origin.y,
                                 self.frame.size.width+100,
                                 self.frame.size.height);
  [UIView commitAnimations];
}

@end

我从TestView类型的视图控制器创建一个插座并连接它并在IB中设置其类型。然后我创建一个按钮,其处理程序调用[customView go];似乎工作正常。

答案 1 :(得分:0)

好的,我现在担心了。问题是我的视图是UIButton,我使用setBackground:forState:setTitle:forState:设置标题和背景。 看来该视图的背景是不可动画的。我使用的工作是用UIImageView添加自己的背景。

感谢您的回复!

相关问题