objective-c – 将uiimage拖放到另一个uiimageview中

objective-c – 将uiimage拖放到另一个uiimageview中,第1张

概述我正在使用以下代码片段来拖放uiimageview UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];[panRecognizer setMinimumNumberOfTouches:1];[panRecogni 我正在使用以下代码片段来拖放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中所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1080648.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存