我想移动一个按钮,这是我的代码。我甚至没有得到" fires1"当我尝试移动按钮时输出。它只是受到压力而且不会移动。我一直在搜索,我总是得到相同的答案,我已经将其应用到我的代码中。
任何人都知道为什么我的按钮不会移动?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
NSLog(@"%@",@"fires1");
if ([touches count] == 1) {
// one finger
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
for (UIButton *iView in self.letterButtons) {
// 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.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];
NSLog(@"%@",@"works!");
}
//}
}
}
}
- (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.currentTitle = self.dragObject.currentTitle;
}
self.dragObject.frame = CGRectMake(self.homePosition.x, self.homePosition.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);
}
答案 0 :(得分:0)
如果你正在使用UIButton,我不明白你为什么要开始实施接触? 删除触摸开始方法并将TouchDown事件上的所有按钮链接到此IBAction
-(IBAction)buttonPressed:(id)sender
{
self.dragObject = (UIButton*)sender;
self.homePosition = CGPointMake(sender.frame.origin.x,
sender.frame.origin.y);
[self.view bringSubviewToFront:self.dragObject];
}
调整你的touceshMoved方法,如
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
self.dragObject.center = currentPoint;
}
让你的touchesEnded方法看起来像这样
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
BOOL btnAIntersectsWithBtnB = CGRectIntersectsRect(self.dragTarget.frame,self.dropObject.frame);
if(btnAIntersectsWithBtnB)
{
self.dropTarget.currentTitle = self.dragObject.currentTitle;
}
self.dragObject.frame = CGRectMake(self.homePosition.x, self.homePosition.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);
}