将uiimage拖放到另一个uiimageview中 [英] drag and drop uiimage into another uiimageview

查看:161
本文介绍了将uiimage拖放到另一个uiimageview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码片段来拖放uiimageview

I am using following code snippet to drag and drop uiimageview

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[myImageView addGestureRecognizer:panRecognizer];

-(void)move:(id)sender {

    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

        firstX = [myImageView  center].x;
        firstY = [myImageView  center].y;
    }

    translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
    [myImageView  setCenter:translatedPoint];

}

此代码拖动整个myImageView,但我的要求是只是拖动uiimage并将其放入另一个uiimagview.myImageView应该保持原样拖后也。我需要拖动myImageView layer.draggable图像应该是透明的。任何想法都会赞赏。

This code is drags the whole myImageView ,but my requirement is to just drag the uiimage and drop it into another uiimagview.myImageView should stay as it is after dragging also.just I need to drag the myImageView layer.draggable image should be transparent. Any ideas would b appreciated.

推荐答案

我已经付出了很多努力来实现您的输出。尝试

I have put little effort to achieve your output. try it

步骤1 :在 .h 文件

UIImageView *ivSource1, *ivDestination2, *tempIV;

步骤2 :初始化所有三个 UIImageView 并添加到您的 ViewController 中写入 viewDidLoad / p>

Step 2 : Initialize all the three UIImageView and add to your ViewController write it in viewDidLoad method

ivSource1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
[ivSource1 setFrame:CGRectMake(100, 100, 100, 100)];
[ivSource1 setTag:100];
[ivSource1 setUserInteractionEnabled:YES];    
[self.view addSubview:ivSource1];

ivDestination2 = [[UIImageView alloc] init];
[ivDestination2 setFrame:CGRectMake(200, 300, 100, 100)];
[ivDestination2 setTag:101];
[ivDestination2 setUserInteractionEnabled:YES];
[self.view addSubview:ivDestination2];

tempIV = [[UIImageView alloc] init];
[tempIV setFrame:CGRectMake(0, 300, 100, 100)];
[tempIV setTag:102];
[tempIV setUserInteractionEnabled:YES];
[self.view addSubview:tempIV];

步骤3 :定义以下触摸方式处理Drag&删除

Step 3 : Define following touch methods to handle movement of image for Drag & Drop

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    if([[touch view] tag] == 100)
    {
        [tempIV setImage:ivSource1.image];
        [tempIV setCenter:[touch locationInView:self.view]];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    [tempIV setCenter:[touch locationInView:self.view]];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    [tempIV setCenter:[touch locationInView:self.view]];

    if(CGRectContainsPoint(ivDestination2.frame, [touch locationInView:self.view]))
    {
        [ivDestination2 setImage:tempIV.image];
    }
    // Remove image from dragable view
    [tempIV setImage:[UIImage imageNamed:@""]];    
}

这篇关于将uiimage拖放到另一个uiimageview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆