大家好,
我第一次使用“触摸方法”面对某些问题。 在我的应用程序中,mainview中有2个子视图。我正在尝试将uibutton从一个视图拖放到另一个视图。 我正在尝试很多东西,但不能拖拉。
请帮帮我。
提前致谢。
My Code :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];
for (UIButton *iView in self.view.subviews) {
if ([iView isMemberOfClass:[UIButton 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.btnalpha = iView;
touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x,
touchPoint.y - iView.frame.origin.y);
homePosition = CGPointMake(iView.frame.origin.x,
iView.frame.origin.y);
[self.viewblue bringSubviewToFront:self.btnalpha];
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];
CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
touchPoint.y - touchOffset.y,
self.btnalpha.frame.size.width,
self.btnalpha.frame.size.height);
self.btnalpha.frame = newDragObjectFrame;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];
if (touchPoint.x > self.vieworange.frame.origin.x &&
touchPoint.x < self.vieworange.frame.origin.x + self.vieworange.frame.size.width &&
touchPoint.y > self.vieworange.frame.origin.y &&
touchPoint.y < self.vieworange.frame.origin.y + self.vieworange.frame.size.height )
self.btnalpha.frame = CGRectMake(self.homePosition.x, self.homePosition.y, self.btnalpha.frame.size.width, self.btnalpha.frame.size.height);
}
- (void) drag
{
str_scramble =@"IH";
for (int i = 0; i < 2; i++)
{
self.btnalpha = [[UIButton alloc] init];
[self.btnalpha setFrame:CGRectMake(i * 50 + 10, 5, 42, 42)];
char letter = [str_scramble characterAtIndex:i];
self.btnalpha.backgroundColor = [UIColor grayColor];
self.btnalpha.titleLabel.font = [UIFont boldSystemFontOfSize:20];
[self.btnalpha setTitle:[NSString stringWithFormat:@"%c", letter] forState:UIControlStateNormal];
[self.btnalpha addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];
[self.btnalpha addTarget:self action:@selector(touchesMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[viewblue addSubview:self.btnalpha];
NSLog(@"string : --> %@", self.btnalpha.titleLabel.text);
}
}
答案 0 :(得分:0)
你应该使用touchDrag方法,如touchDragEnter,touchDragExit,touchDragInside或touchDragOutside来拖动UIButton。
这些所有事件都是UIControlEvents
答案 1 :(得分:0)
我正在使用PanGestureRecognizer,效果很好。
UIPanGestureRecognizer *drag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragButton:)];
drag.delegate = self;
drag.view.tag = position;
[button addGestureRecognizer:drag];
- (void)dragButton:(UIPanGestureRecognizer *)gesture
{
buttonPicked = [allObjects objectAtIndex:gesture.view.tag -1];
if (gesture.state == UIGestureRecognizerStateBegan)
{
originalPoint = buttonPicked.center;
}
else
{
buttonPicked.center = [gesture locationInView:self.view];
}
}
如果你想对结束手势进行某种特殊处理(例如只在特定位置丢弃) - 你可以使用UIGestureRecognizedStateEnd。
如果它仍然无效 - 在UIGestureRecognizerStateBegan中,尝试从子视图中删除该按钮并将其添加到主视图中,并在UIGestureRecognizerStateEnd中检查该按钮所在的子视图并添加它而不是主视图视图。应该做的伎俩。
答案 2 :(得分:0)
根据关于事件处理指南的Apple文档,
当你第一次触摸按钮时,它变成第一个响应者.SO,touchesBegan:你的ViewController中的withEvent没有被调用。
禁用UI按钮的userInteraction,现在viewcontroller将成为第一响应者并开始接收触摸事件。
button.userInteractionEnabled = NO;
尽管你将GetGestures添加到ViewController或其中任何一个视图,但当你的触摸按下按钮时它们不会响应。