在CALayer的坐标系统检测触摸事件 [英] Detect touch event in CALayer's coordinate system

查看:175
本文介绍了在CALayer的坐标系统检测触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我控制器在哪里随机掉落大量使用 CAKeyframeAnimation 图片,我应该通过触摸轨迹裁剪图像此,
使用的CALayer 为present的动画形象的动画,我试图使用来检测触摸事件内这层 [层presentationLayer ]

问题是 - 对于剪裁此图片我应该创建从我接触跟踪段和层路径,我不弄清楚但我怎么可以创建此路径,但这里的问题是,如何在下降<$检测到这一接触点C $ C>的CALayer 坐标系统,附加的图片更多的信息。

任何想法?

图片说明

有关检测与控制相关层的坐标系我用这code触摸点:

   - (无效)touchesMoved:(*的NSSet)触及:(CGPoint)movingPoint:(*的UIEvent)事件
{
    的NSArray *层= [[contextView层]子层];
    对于(分层的CALayer *层){
            的CGRect imageRect = [[层presentationLayer]框]。
            如果(CGRectContainsPoint(imageRect,movingPoint)){
                的NSLog(@图像位置 - X%F Y%F,movingPoint.x,movingPoint.y);
            }
    }
}


解决方案

正如你可能知道,你的接收点视图的坐标系中,一般应与视图的主层的坐标系。 (如果没有,还是有办法将其转换,但除非你已经做了一些奇怪的,它更容易只是依靠事实,这些都是一样的。)

同样重要的是要知道,一旦你已经开始旋转的东西,它的是不确定的。如果你想想框架是如何工作的一点点,应该是显而易见的,为什么这必须的情况下(使用未旋转矩形你不能定义一个钻石)。

我们可以很容易地转换从一个系统到另一个使用 convertPoint:fromLayer:。没有 touchesMoved:movingPoint:在iOS版方法,所以我假定这就是你已经自己的坐标系中摸索出的一些点自定义的方法。所以,你想要的东西,如:

  CGPoint pointInLayer = [[层presentationLayer] convertPoint:movingPoint fromLayer:self.view.layer];
的CGRect layerBounds = [[层presentationLayer]界限]
如果(CGRectContainsPoint(layerBounds,pointInLayer)){
    //相交!
}

边界始终定义,因为它们在层自己的坐标系总是。因此,我们转换成点图层的坐标系,并询问是否这点在其边界存在。

I have controller where are randomly falling a lot of pictures using CAKeyframeAnimation and I should crop this images by touching track, any animations using CALayer for present the animated image, and I am trying to detect touch event inner this layer using [layer presentationLayer].

The problem is - for cropping this image I should create paths from my touching tracker segment and layer, I don't figure out yet how I can create this paths but the question here is how detect this touch point in falling CALayer coordinate system, attached picture more informative.

Any ideas?

For detecting touch point in layer related with controller coordinate system I am using this code:

- (void) touchesMoved:(NSSet *)touches :(CGPoint) movingPoint :(UIEvent *)event
{
    NSArray *layers = [[contextView layer] sublayers];
    for (CALayer *layer in layers) {
            CGRect imageRect = [[layer presentationLayer] frame];
            if(CGRectContainsPoint(imageRect, movingPoint)) {
                NSLog(@"Image position - x %f y %f", movingPoint.x, movingPoint.y);
            }
    }
}

解决方案

As you likely know, the point your receive is in the view's coordinate system, which should generally be identical to the view's main layer's coordinate system. (If not, there are still ways to convert it, but unless you've done something weird, it's easier just to rely on the fact that these are the same.)

It's also important to know that once you've started rotating something, its frame is undefined. If you think a little bit about how frames work, it should be obvious why this has to be the case (you can't define a diamond using an unrotated rectangle).

We can easily convert from one system to the other using convertPoint:fromLayer:. There is no touchesMoved:movingPoint: method in iOS, so I'm assuming this is some custom method where you've already worked out the point in your own coordinate system. So you'd want something like:

CGPoint pointInLayer = [[layer presentationLayer] convertPoint:movingPoint fromLayer:self.view.layer];
CGRect layerBounds = [[layer presentationLayer] bounds];
if (CGRectContainsPoint(layerBounds, pointInLayer)) {
    // Intersect!
}

The bounds are always defined, since they're always in a layer's own coordinate system. So we convert the point into the layer's coordinate system and ask if this point exists in its bounds.

这篇关于在CALayer的坐标系统检测触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆