开发者

Drag copy between UIViews

开发者 https://www.devze.com 2023-04-04 12:18 出处:网络
I am essentially trying to do what is covered in this question, but copy instead of move. Here is what the view hierarchy looks like (copied from the existing question). A and B are both inside a com

I am essentially trying to do what is covered in this question, but copy instead of move.

Here is what the view hierarchy looks like (copied from the existing question). A and B are both inside a common superview. X is a view inside A.

  A        B
_____    _____
|   |    |   |
| X | -> |   |
|___|    |___|

I want to copy X into B. A is a list of available items. I want to choose an item from A and add a copy of it to B.

This is what I have come up with so far. After detecting a touch in X, I make a copy of the view (viewCopy). Then I add viewCopy to A and add the original开发者_StackOverflow社区 view to the superview of A and B. This is because the touches are probably active on the original view and not on the copied view. I want the new (copied) view to respond to drag for a smooth transition from A to B.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{        
    id viewCopy = [self mutableCopy];
    [self.superview.superview addSubview:self];
    [self.superview addSubview:viewCopy];
    [viewCopy release];
}

This obviously does not work. It is a very crude try, but I am not sure how to go about doing this. Any help would be great. Thanks.


UIView doesn’t adhere to the NSCopying protocol, so attempting to copy it is likely failing—it’s hitting the base NSObject -mutableCopy, which doesn’t do what you want. To get your clone view, you’ll have to recreate it. Easiest way to handle that is to either set up your X view so it can load from a XIB or implement it as a custom UIView subclass that can set up its own contents.

For an example of the former, check out the docs; the example is for loading custom table view cells, but the technique’s easily applicable to a generic custom view.

0

精彩评论

暂无评论...
验证码 换一张
取 消