将图像从UITableviewCell拖放到其他视图

时间:2013-01-11 05:30:39

标签: ios objective-c cocoa-touch drag-and-drop

我正在使用iPhone应用,我需要从UITableViewCell中拖动图像 并将其放在tableview外的其他UIView

(imageview添加为:[cell.contentView addSubview:imageView])。

请帮我这个,任何有用的提示,示例代码?触摸事件委托不会触发,因为我将UIImageView添加到UITableViewCell。

1 个答案:

答案 0 :(得分:1)

UIImageView你不会得到太多的事件所以请使用UIButton并尝试这样的事情来拖动:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(imageTouch:withEvent:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button setImage:[UIImage imageNamed:@"vehicle.png"] forState:UIControlStateNormal];
[self.view addSubview:button];

- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
    UIControl *control = sender;
    control.center = point;
}

当您的中心点与其他UIView使用[UIView adSubview:]方法匹配时,在UIView中添加UIButton

我从Basic Drag and Drop in iOS

获取此代码

查看:Drag an Image within the Bounds of Superview