如果使用CGRect Variable代替CGRectMake,则动画不起作用

时间:2012-08-14 21:18:02

标签: objective-c ios cocoa-touch uiviewanimation

我有一个动画,将图像向右移动,然后在我松开按钮时停止。我需要图像的帧在不同的时间是不同的,所以我使用CGRect变量代替myImageView.frame = CGRectMake。但是当我每次放开时使用变量图像跳回到其原始位置而不是像往常一样停在原位。

if (myInt == 2) {
    MyCGRect = CGRectMake(myImageView.frame.origin.x,myImageView.frame.origin.y,18,42);
} else {
    MyCGRect = CGRectMake(myImageView.frame.origin.x,myImageView.frame.origin.y,35,28);
}

-(IBAction)holdToMakeImageMove:(id)sender
{
//!!!!!This Works!!!!!!
myImageView.frame = CGRectMake(myImageView.frame.origin.x,myImageView.frame.origin.y,18,42);

//!!!!!!THIS doesnt work

myImageView.frame = MyCGRect;

//...animation

}

1 个答案:

答案 0 :(得分:-1)

例如,此代码适用于我。将按钮框从(0,800,50,50)更改为(0,0,100,100)

@interface ViewController ()
...
@property (nonatomic, assign) CGRect myRect;
...
@end

@implementation ViewController
...
@synthesize myRect = _myRect;
...
- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setFrame:CGRectMake(0, 800, 50, 50)];
    [btn addTarget:self action:@selector(avvia:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

    self.myRect = CGRectMake(100, 0, 100, 100);
}
...
- (void)avvia:(id)sender {
    ((UIButton *)sender).frame = self.myRect;
}
...
@end