0

我有一个动画将图像向右移动,然后在我松开按钮时停止。我需要图像的帧在不同时间有所不同,所以我使用 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

}
4

1 回答 1

-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
于 2012-08-14T21:32:56.237 回答