我有两个CAShapeLayers进入UIView的主要层.CAShapeLayers具有复杂的形状,我需要知道在形状边界内是否触摸了一个点.另外,我需要知道哪个形状被触及了.
我正在尝试containsPoint,但没有任何作用.
cocoa-touch core-graphics quartz-graphics uikit cashapelayer
我有一个CAShapeLayer,它必须做一个简单的任务,在用户的手指指导下移动屏幕.
问题是运动太慢了.该层确实移动,但有一个滞后,感觉很慢.
我有另一个测试应用程序,其中移动UIImage并且没有任何延迟,图像立即移动.
我该怎么做才能克服这个问题?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
currentPoint = [[touches anyObject] locationInView:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint activePoint = [[touches anyObject] locationInView:self];
CGPoint newPoint = CGPointMake(activePoint.x - currentPoint.x,activePoint.y - currentPoint.y);
curLayer.position = CGPointMake(shapeLayer.position.x+newPoint.x,shapeLayer.position.y+newPoint.y);
currentPoint = activePoint;
}
谢谢!
我在视图中有一个UIBezierPath.我想在点击形状时显示警告,但实际发生的情况是警报不仅在我单击形状时显示,而且在我单击视图中的任何位置时显示.
如何更改此代码,以便只有当我在彩色形状内部单击时,才会收到警报?另外,如何在屏幕上绘制可拖动的形状?
#import "draw2D.h"
@implementation draw2D
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}
- (void)drawRect:(CGRect)rect {
UIBezierPath* aPath = [UIBezierPath bezierPath];
[aPath moveToPoint:CGPointMake(200.053,79.688)];
[aPath addLineToPoint:CGPointMake(100.053,179.688)];
[aPath addLineToPoint:CGPointMake(304.412,280.125)];
[aPath addLineToPoint:CGPointMake(308.055,298.513)];
[aPath addLineToPoint:CGPointMake(200.053,79.688)];
[aPath closePath];
[[UIColor blackColor] setStroke];
[[UIColor redColor] setFill];
CGContextRef aRef = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(aRef, 50, 50);
aPath.lineWidth = 5;
[aPath fill];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Some message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
//After …Run Code Online (Sandbox Code Playgroud) 我无法理解为什么CAShapeLayer不响应hitTest
这个功能总是去//触摸外面
如何检测CAShapeLayer上的触摸?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
currentPoint = [[touches anyObject] locationInView:self];
for (CAShapeLayer *layer in self.layer.sublayers) {
if(layer == shapeLayer) {
if([layer hitTest:currentPoint])
{
// touche is on the layer
}
else {
// touche is outside
}
}
}
}