将名称传递给 UIView 的子类

发布于 2024-10-31 03:39:28 字数 693 浏览 4 评论 0原文

我创建了 UIView 的子类,以便处理 hitTest 和 Touches 操作,因为 UIView 是 UIScrollview 的子视图,并且我需要 UIView 和 UIScrollView 都是可拖动的。

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

我的问题是,由于我在 UIScrollview 上有多个子类 UIView 实例,我如何判断哪个实例被拖动了。

在之前的应用程序中,我只是使用了;

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

但由于它位于我的主应用程序 .m 文件中,所以这是可以的,因为 blah 和 fred 都是在 viewdidload 中创建的,但由于我的代码中的触摸事件是在子类中捕获的,所以它们不知道 blah 和 fred。

所以1)我如何让子类知道我添加的项目 - 或 -
2) 我如何将项目的名称传递给 hitTest 或 TouchesBegan 中的子类 - 或 -
3)是否有一行代码来获取当前项目的名称

任何帮助非常感谢。

I have created a subclass of UIView in order to handle hitTest and Touches actions, as the UIView is a subview of a UIScrollview and I needed both the UIView and UIScrollView to be draggable.

I have got it 95% working - the subclassed UIView works perfectly if I jsut set a background colour - but if I set a subview with an image with transparanecy it doesn't always respond. I have some ideas on this so thats not the question.

My question is that as I have multiple instances of the subclassed UIView on the UIScrollview, how can i tell which one is dragged.

In a previous app I simply used;

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

but as this sits in my main application .m file this was ok as blah and fred were both created there in viewdidload, but as the touch events in my code are captured in the subclass they don't know about blah and fred.

so 1) how do i let the subclass know about the items i've added - or -
2) how do i pass the name of the item through to the subclass in either hitTest or TouchesBegan - or -
3) is there a line of code to get the name of the current item

Any help much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱倦旧时光 2024-11-07 03:39:28

好的,这就是您可以使用通知中心轻松完成此操作的方法。

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

这是代码:

需要了解拖动的对象在其 init 方法中注册以下内容:

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

发现拖动后执行操作的方法:

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

    // do what ever you want with the object
}

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

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

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

希望这有帮助。

Ok this is how you could do it easily with NotificationCenter.

The object that needs to know which subview is dragged can listen to the notification named let's say "SubviewIsDragged" and each subview can send the notification "SubviewIsDragged" passing itself as a parameter.

This is the code:

Object that needs to know about dragging registers in it's init method the following:

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

Method that performs actions after dragging has been discovered:

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

    // do what ever you want with the object
}

Now in your UIView subclass add the following to your dragging method:

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

Notice that you pass object "self" but you can pass any other object you want like NSString ID.

Hopes this help.

静待花开 2024-11-07 03:39:28

将 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]];
}
}

add a panGesture recognizer to your subview class

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

use this code for the translation and you wouldn't have any problems with dragging the wrong view

- (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]];
}
}
剩余の解释 2024-11-07 03:39:28

对于其他也有同样问题的人来说。

我找到了一个简单的解决方案:

在我的 viewDidLoad 中,当我添加子类化的子视图时,我只需添加行
自定义View1.tag = 111;

然后在我的 Touches 方法的子类中我可以使用:
if ([touch view].tag == 111) {

简单! :)

So for anyone else who also has the same problem.

I found a simple solution:

In my viewDidLoad when I added the subclassed subviews, I just add in the line
customView1.tag = 111;

then in the subclass within my touches methods I can just use:
if ([touch view].tag == 111) {

Simple! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文