1.核心思路
左右移动其实就是一个图片按照指定的坐标点进行位移,我们只要让图片的中心点按定制好的路径移动就可以了,话不多说,直接上代码
(1)UIImage *backImage = [UIImage imageNamed:@"login_backImage"];
_countArr = [NSMutableArray array];
for (int i = 0; i < (backImage.size.width - FACT_WIDTH) * 0.5; i++) {
NSString *str = NSStringFromCGPoint(CGPointMake(FACT_WIDTH * 0.5 + i, _backView.height * 0.5));
[_countArr addObject:str];
}
for (int i = (int)_countArr.count - 1; i > 0; i--) {
NSString *str = _countArr[i];
[_countArr addObject:str];
}
for (int i = 0; i < (backImage.size.width - FACT_WIDTH) * 0.5; i++) {
NSString *str = NSStringFromCGPoint(CGPointMake(FACT_WIDTH * 0.5 - i, _backView.height * 0.5));
[_countArr addObject:str];
}
[self protagonistMove:_countArr protagonist:_backView number:0];
(2)-(void)protagonistMove:(NSArray*)PointArray protagonist:(UIImageView*)protagonist number:(NSInteger)number
{
CGPoint point = CGPointFromString([PointArray objectAtIndex:number]);
double durationTime = [self distanceBetweenTwoPoint:protagonist.center point2:point]/MOVE_VELOCITY;
[UIView animateWithDuration:durationTime delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
protagonist.center = point;
}completion:^(BOOL finished)
{
if (finished) {
NSUInteger currentPointNumber = (number + 1) == self.countArr.count ? 0 : number + 1;
[self protagonistMove:PointArray protagonist:protagonist number:currentPointNumber];
}
}];
}
(3)- (int)distanceBetweenTwoPoint:(CGPoint)onePoint point2:(CGPoint)twoPoint
{
double dis = sqrt ((onePoint.x - twoPoint.x) * (onePoint.x - twoPoint.x) + (onePoint.y - twoPoint.y) * (onePoint.y - twoPoint.y));
return dis;
}
2.把上面的所以的代码拷贝到你的项目中即可,但是这种方法性能感觉不是很好,由于项目进度赶的比较急,所以后期再做优化。