我为使用触摸调整 CALayer 大小而编写的一些代码遇到了一些性能问题。它工作正常,但动画还远远不够活泼,并且落后于触摸位置。
CGPoint startPoint;
CALayer *select;
- (CGRect)rectPoint:(CGPoint)p1 toPoint:(CGPoint)p2 {
CGFloat x, y, w, h;
if (p1.x < p2.x) {
x = p1.x;
w = p2.x - p1.x;
} else {
x = p2.x;
w = p1.x - p2.x;
}
if (p1.y < p2.y) {
y = p1.y;
h = p2.y - p1.y;
} else {
y = p2.y;
h = p1.y - p2.y;
}
return CGRectMake(x, y, w, h);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *t1 = [[[event allTouches]allObjects]objectAtIndex:0];
CGPoint loc = [t1 locationInView:self];
startPoint = loc;
lastPoint = CGPointMake(loc.x + 5, loc.y + 5);
select = [CALayer layer];
select.backgroundColor = [[UIColor blackColor]CGColor];
select.frame = CGRectMake(startPoint.x, startPoint.y, 5, 5);
[self.layer addSublayer:select];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *t1 = [[[event allTouches]allObjects]objectAtIndex:0];
CGPoint loc = [t1 locationInView:self];
select.bounds = [self rectPoint:startPoint toPoint:loc];
}
有没有更好的方法来实现这一目标?