我正在使用以下代码片段来拖放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:@""]]; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。