今天在做动画的时候,发现一个问题这是我的源代码
[UIView animateWithDuration:0.7 animations:^{
self.image1.transform = CGAffineTransformMakeTranslation(10, 17);
self.image2.transform = CGAffineTransformMakeTranslation(-10, -17);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{
self.image1.transform = CGAffineTransformMakeTranslation(-10, -17);
self.image2.transform = CGAffineTransformMakeTranslation(10, 17);
} completion:^(BOOL finished) {
}];
我的意思是来回移动位置,但是在回来的时候,移动了很长的一段距离,一直没有搞明白,
查了一下资料
CGAffineTransformMakeTranslation
这个是基于原始位置,也就说是起始位置一直没有变化,只是位移变了 我在第二次的时候误将起始位置认为是移动后的位置,所以出现这个错误,这样的话就换一种思维方式,让它在离自己远点的区间中移动每次不要回到原点就行
下面是代码
- (void)viewDidLoad {
[super viewDidLoad];
view = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[self.view addSubview:view];
view.backgroundColor = [UIColor redColor];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(addAdmin) userInfo:nil repeats:YES];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)addAdmin
{
[UIView animateWithDuration:0.5 animations:^{
view.transform = CGAffineTransformMakeTranslation(0, 100);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{
view.transform = CGAffineTransformMakeTranslation(0, 30);
} completion:^(BOOL finished) {
}];
}];
}
这样相当于在区间里面30 --100 之间移动也可以达到自己想要的效果。