UIBezierPath:appendPath 方法丢失有关每个路径的信息

发布于 2024-12-02 22:58:47 字数 1076 浏览 2 评论 0原文

问题是这样的...

我创建了一个扩展 UIBezierClass 的类,名为 PathExtended,其中我添加了 NSString ID;

那么,我有一个 PathExtended 数组。 在drawrect方法中我写道:

p = [[PathExtended alloc] init];

for (int i=0; i<[arrayOfPaths count]; i++) {

    [p appendPath:[arrayOfPaths objectAtIndex:i]];
    [p closePath];
}

[p applyTransform:CGAffineTransformMake(a, b, c, d, e, f)];
[p fill];

现在,如果我在touchend方法中测试:

if ([p containsPoint:pointTouched]) {
            NSLog(@"There is!");
        }

那就可以了!!!相反,如果我测试:

if ([p containsPoint:pointTouched]) {
            NSLog(@"ID= %@", p.ID);
        }

日志是空白的!

我可以理解为什么会发生这种情况,但是,我不明白如何解决问题。 我认为appendPath创建了一个唯一的路径,因此,每个单独的路径信息,如ID,都会丢失。 我还认为,如果我在不使用appendPath方法的情况下绘制每条路径,我可以解决问题,但是......我不知道......似乎我遵循了错误的方式。

有什么想法吗??? 抱歉我的英语(我是意大利人:P)

编辑:

PathExtended .h

@interface PathExtended : UIBezierPath {

NSString* ID;
}

@property (nonatomic, readwrite) NSString* ID;

-(id) initwithID:(NSString*) _ID;

the problem is this...

I created a class that extends UIBezierClass called PathExtended, in which i added NSString ID;

I have, then, an array of PathExtended.
In the drawrect method i wrote:

p = [[PathExtended alloc] init];

for (int i=0; i<[arrayOfPaths count]; i++) {

    [p appendPath:[arrayOfPaths objectAtIndex:i]];
    [p closePath];
}

[p applyTransform:CGAffineTransformMake(a, b, c, d, e, f)];
[p fill];

Now, if i test in the touchend method:

if ([p containsPoint:pointTouched]) {
            NSLog(@"There is!");
        }

It's ok!!! Instead, if i test:

if ([p containsPoint:pointTouched]) {
            NSLog(@"ID= %@", p.ID);
        }

Log is blank!!

I can understand why it happens, but, i can't understand how solving problem.
I thought that appendPath creates a unique path, so, each single path information, like ID, is lost.
I thought also that, if i draw each path without using appendPath method i can solve the problem, but... i don't know... it seems as if i'm following the wrong way.

Any idea???
Sorry for my english (i'm italian :P)

EDIT:

PathExtended .h

@interface PathExtended : UIBezierPath {

NSString* ID;
}

@property (nonatomic, readwrite) NSString* ID;

-(id) initwithID:(NSString*) _ID;

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

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

发布评论

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

评论(1

星星的轨迹 2024-12-09 22:58:47

想象一下,如果您创建 NSDecimalNumber 的子类,将其命名为 MyImaginaryNumber,这样您的子类存储了一个虚构的组件,会发生什么。如果您尝试调用像 -decimalNumberByAdding: 这样的方法会发生什么?您不会真的期望 NSDecimalNumber 的该方法的实现将您的虚部包含在结果数字中,对吗? NSDecimalNumber 显然对虚部一无所知,因此不能期望它做正确的事情。

如果您希望 -appendPath: 与您的 PathExtended 对象一起使用,则需要使用保留 PathExtended 中额外信息的实现来覆盖 -appendPath:。当然,如果有用的话,您仍然可以从自己的内部调用 UIBezierPath 的实现。例如,如果您的 ID 是可组合的,您可能会执行以下操作:

-(PathExtended*)appendPath:(PathExtended*)path
{
    PathID newID = [self composeID:path.ID];
    PathExtended *newPath = [PathExtended pathWithBezierPath:[super appendPath:path] ID:newID];
    return newPath;
}

我不确定为什么您的日志消息根本没有打印(如果确实发生了这种情况),但这很可能是由于 UIBezierPath 没有 ID< /代码> 属性。

Imagine what would happen if you created a subclass of NSDecimalNumber, call it MyImaginaryNumber, such that your subclass stored an imaginary component. What would happen if you tried to call a method like -decimalNumberByAdding:? You wouldn't really expect NSDecimalNumber's implementation of that method to include your imaginary part in the resulting number, would you? NSDecimalNumber obviously doesn't know anything about the imaginary part, so it can't be expected to do the right thing.

If you want -appendPath: to work with your PathExtended objects, you'll need to override -appendPath: with an implementation that preserves the extra information in PathExtended. You can still call UIBezierPath's implementation from inside your own if that's useful, of course. For example, if your ID's are composable, you might do something like:

-(PathExtended*)appendPath:(PathExtended*)path
{
    PathID newID = [self composeID:path.ID];
    PathExtended *newPath = [PathExtended pathWithBezierPath:[super appendPath:path] ID:newID];
    return newPath;
}

I'm not sure why your log message didn't print at all, if that's what happened, but it's likely due to UIBezierPath not having an ID property.

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