我想补充拖放的拖放功能UIButton
。 其实我已经编程方式下创建按钮的数量UIScrollView
。 我想补充的目标之后UIButton
[mybutton addTarget:self action:@selector(wasDragged:withEvent:)forControlEvents:UIControlEventTouchDragInside];
这是该方法
- (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);
}
该代码被滚动视图下工作正常和按钮拖放滚动视图的内部。 现在的问题是,当我拖动滚动视图外的按钮,按钮消失。 我想滚动视图外拖动按钮也。 我怎样才能做到这一点? 我怎样才能拖动按钮从滚动型到另一个的UIView? 这怎么可能?
ViewController.h
@interface ViewController : UIViewController <UIScrollViewDelegate>{
}
@property (nonatomic, strong) IBOutlet UIView *dropTarget;
@property (nonatomic, strong) UIImageView *dragObject;
@property (nonatomic, retain) UITextView *txt_View;
@property (nonatomic, strong) UIImageView *prevdragObject;
@property (nonatomic, assign) CGPoint touchOffset;
@property (nonatomic, assign) CGPoint homePosition;
@end
只是代替
IBOutlet UIView *dropTarget
只是使用
IBOutlet UIScrollView *dropTarget
ViewController.m
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 1) {
// one finger
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
for (UIImageView *iView in self.view.subviews) {
if ([iView isMemberOfClass:[UIImageView class]]) {
if (touchPoint.x > iView.frame.origin.x &&
touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
touchPoint.y > iView.frame.origin.y &&
touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
{
self.dragObject = iView;
self.touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x,
touchPoint.y - iView.frame.origin.y);
self.homePosition = CGPointMake(iView.frame.origin.x,
iView.frame.origin.y);
[self.view bringSubviewToFront:self.dragObject];
}
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
touchPoint.y - touchOffset.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);
self.dragObject.frame = newDragObjectFrame;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
if (touchPoint.x > self.dropTarget.frame.origin.x &&
touchPoint.x < self.dropTarget.frame.origin.x + self.dropTarget.frame.size.width &&
touchPoint.y > self.dropTarget.frame.origin.y &&
touchPoint.y < self.dropTarget.frame.origin.y + self.dropTarget.frame.size.height )
{
self.dropTarget.backgroundColor = [UIColor colorWithPatternImage:self.dragObject.image];
}
self.dragObject.frame = CGRectMake(self.homePosition.x, self.homePosition.y, self.dragObject.frame.size.width, self.dragObject.frame.size.height);
self.prevdragObject.frame = self.dragObject.frame;
}
我通过改变代码解决了这个问题
[self.scrollView addSubview:myButton];
至
[self.view addSubview:myButton];
您需要将您的拖动逻辑最多包含滚动视图和其他视图的视图。 当用户开始在更高的观点拖累,你确定哪些子视图低于它。 然后重新分配视图(按钮)为这个更高的视图,而不是原来的父视图(滚动视图)的子视图。 取决于是拖动完成时,按钮被添加回滚动视图或其它视图被拖动到。