拖曳从UIScrollView将UIButton从Objective C中删除到UIView [英] Drag & Drop UIButton from UIScrollView to UIView in Objective C

查看:147
本文介绍了拖曳从UIScrollView将UIButton从Objective C中删除到UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加拖拽在 UIButton 上删除功能。实际上,我已经在 UIScrollView 下以编程方式创建了按钮。我在中添加以下目标UIButton

  [mybutton addTarget:self action: @selector(wasDragged:withEvent:方法)forControlEvents:UIControlEventTouchDragInside]; 

这是方法



<$ p $ (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
//获取触摸

UIButton * myButton = [[UIButton alloc] init];
myButton = button;
[self.scrollView addSubview:myButton];

UITouch * touch = [[event touchesForView:button] anyObject];

//获取delta
CGPoint previousLocation = [touch previousLocationInView:myButton];
CGPoint location = [touch locationInView:myButton];
CGFloat delta_x = location.x - previousLocation.x;
CGFloat delta_y = location.y - previousLocation.y;

//移动按钮
myButton.center = CGPointMake(myButton.center.x + delta_x,myButton.center.y + delta_y);

}

此代码在滚动视图下正常工作,按钮为拖曳放在scrollview里面。现在的问题是当我将滚动按钮拖动到scrollview之外,按钮消失。
我也想在scrollview外拖动按钮。我该怎么做?
如何将ScrollView拖动到另一个UIView?如何可能?

解决方案

我通过更改代码解决了这个问题

  [self.scrollView addSubview:myButton]; 

  [self.view addSubview:myButton]; 


I Want to add drag & drop functionality on UIButton. Actually I have created numbers of buttons programmatically under UIScrollView. I add following target on UIButton

[mybutton addTarget:self action:@selector(wasDragged:withEvent:)forControlEvents:UIControlEventTouchDragInside];

and this is the method

- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
 {
// get the touch

UIButton *myButton=[[UIButton alloc]init];
myButton=button;
[self.scrollView addSubview:myButton];

UITouch *touch = [[event touchesForView:button] anyObject];

//get delta
CGPoint previousLocation = [touch previousLocationInView:myButton];
CGPoint location = [touch locationInView:myButton];
CGFloat delta_x = location.x - previousLocation.x;
CGFloat delta_y = location.y - previousLocation.y;

//move button
myButton.center = CGPointMake(myButton.center.x + delta_x,myButton.center.y + delta_y);

}

This code is working fine under the scroll view and button is drag & drop inside the scrollview. Now problem is when I drag the button outside the scrollview, button disappear. I want to drag button outside the scrollview also. How can I do this? How can I drag button from Scrollview to another UIView? How is it possible?

解决方案

I solved this problem by changing code

[self.scrollView addSubview:myButton];

to

[self.view addSubview:myButton];

这篇关于拖曳从UIScrollView将UIButton从Objective C中删除到UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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