简体   繁体   English

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

[英]Animation doesnt work if CGRect Variable is used in place of CGRectMake

I have an animation that moves an image to the right then stops when I let go of the button. 我有一个动画,它将图像向右移动,然后放开按钮时停止。 I need the frame of the image to be different at different times so I used a CGRect variable in place of myImageView.frame = CGRectMake. 我需要图像的帧在不同时间是不同的,因此我使用了CGRect变量来代替myImageView.frame = CGRectMake。 But when I use the variable image hops back to its original spot every time I let go instead of stoping in place like usual. 但是,当我使用可变图像时,每次放开它都会跳回到其原始位置,而不是像往常一样停在原地。

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

}

For example, this code works for me. 例如,此代码对我有用。 Change button frame from (0,800,50,50) to (0,0,100,100) 将按钮框从(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM