iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了objective-c – 将uiimage拖放到另一个uiimageview中大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_419_6@ 我正在使用以下代码片段来拖放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图像应该是透明的.任何想法都会受到赞赏.

解决方法

我没有花太多精力来实现你的输出.试试吧

第1步:在.h文件中定义这3个变量

UIImageView *ivSource1,*ivDestination2,*tempIV;

第2步:初始化所有三个UIImageView并添加到ViewController中,在viewDidLoad方法中编写它

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

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

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

步骤3:定义以下触摸方法以处理拖动和放大的图像移动.下降

-(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:@""]];    
}

大佬总结

以上是大佬教程为你收集整理的objective-c – 将uiimage拖放到另一个uiimageview中全部内容,希望文章能够帮你解决objective-c – 将uiimage拖放到另一个uiimageview中所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: