view背景图左右移动

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.把上面的所以的代码拷贝到你的项目中即可,但是这种方法性能感觉不是很好,由于项目进度赶的比较急,所以后期再做优化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值