2011-04-08 74 views
0

为了处理hitTest和Touches动作,我创建了UIView的子类,因为UIView是UIScrollview的子视图,我需要UIView和UIScrollView都是可拖动的。将名称传递给UIView的子类

我有95%的工作 - 子类UIView完美的作品,如果我只是设置背景颜色 - 但如果我设置与透明的图像子视图它并不总是响应。我对此有一些想法,所以这不是问题。

我的问题是,因为我有UIScrollview上的子类UIView的多个实例,我怎么知道哪一个被拖动。

在我以前使用的应用程序中;

if ([touch view] == blah) {   
//do something; 
} 
else if ([touch view] == fred) { 
//do nothing; 
} 

但这个坐在我的主要应用程序.m文件,这是不错的方法等等,并在viewDidLoad中佛瑞德都在这里创建的,但在我的代码的触摸事件在子类中被捕获,他们不知道关于等等和弗雷德。

所以1)我如何让子类了解项目我已经添加了 - 我如何通过向任一则hitTest或在的touchesBegan子通过项的名称
2) - - 或或 -
3)是否有一行代码来获取当前项目的名称

任何帮助非常感谢。

回答

0

所以对于谁比谁任何问题也有同样的问题。

我发现了一个简单的解决方案:

以我viewDidLoad中当我添加子类的子视图,我只是添加在线路
customView1.tag = 111;

然后在子类中我接触的方法,我可以只使用内:
如果([触摸视图] .TAG == 111){

简单! :)

0

好吧,这是如何使用NotificationCenter轻松实现的。

需要知道哪个子视图被拖动的对象可以侦听名为“SubviewIsDragged”的通知,每个子视图可以发送通知“SubviewIsDragged”作为参数传递自己。

这是代码:

对象,需要了解一下它拖动寄存器进行的以下init方法:

-(void)subviewIsDragged:(NSNotification *)notification 
{ 
    //Here you can get your subclass of UIView 
    UIView* view = [notification object]; 

    // do what ever you want with the object 
} 
:那拖着已被发现后执行动作

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subviewIsDragged) name:@"SubviewIsDragged" object:nil]; 

方法

现在在您的UIView子类中,将以下内容添加到您的拖动方法中:

[[NSNotificationCenter defaultCenter] postNotificationName:@"SubviewIsDragged" object:self]; 

请注意,您传递对象“self”,但您可以传递任何其他想要的对象,如NSString ID。

希望得到这个帮助。

0

添加panGesture识别到你的子视图类

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)]; 
    [panGesture setMaximumNumberOfTouches:2]; 
    [panGesture setDelegate:self]; 
    [self addGestureRecognizer:panGesture]; 

使用此代码的翻译,你会不会有拖错误的观点

- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer 
{ 

[self adjustAnchorPointForGestureRecognizer:gestureRecognizer]; 

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) { 

    CGPoint translation = [gestureRecognizer translationInView:[self superview]]; 

    self.center = CGPointMake([self center].x + translation.x, [self center].y + translation.y); 
    [gestureRecognizer setTranslation:CGPointZero inView:[self superview]]; 
} 
}