1

我想在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);

}

此代码在滚动视图下运行良好,并且按钮在滚动视图内拖放。现在的问题是当我将按钮拖到滚动视图之外时,按钮消失了。我也想将按钮拖到滚动视图之外。我怎样才能做到这一点?如何将按钮从 Scrollview 拖到另一个 UIView?这怎么可能?

4

3 回答 3

1

视图控制器.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 

视图控制器.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;
}
于 2012-11-01T05:04:24.393 回答
1

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

[self.scrollView addSubview:myButton];

[self.view addSubview:myButton];
于 2012-11-01T12:57:01.840 回答
0

您需要将拖动逻辑向上移动到包含滚动视图和其他视图的视图。当用户在较高视图上开始拖动时,您可以确定哪个子视图在它下面。然后将该视图(按钮)重新分配为该更高视图的子视图,而不是其原始父视图(滚动视图)。根据拖动完成的位置,按钮被添加回滚动视图或另一个视图被拖动到。

于 2012-11-01T05:07:04.870 回答