2013-04-13 177 views
0

我在屏幕上有很多UIbuttons,我希望用户能够通过按住它们然后拖动它们来移动按钮。我已经拿出这个代码:在屏幕上移动UIbutton

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 

[[NSUserDefaults standardUserDefaults] synchronize]; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"MoveAllViewsUpForDrag" object:self userInfo:nil]; 

//[self.layer removeAllAnimations]; 

// Retrieve the touch point 
CGPoint pt = [[touches anyObject] locationInView:self.view]; 
startLocation = pt; 

originYDraggable = self.frame.origin.y; 
originXDraggable = self.frame.origin.x; 

[[self superview] bringSubviewToFront:self]; 

[UIImageView animateWithDuration:0.5 animations:^(void) { 

    CGRect frame = [self frame]; 
    frame.size.width = frame.size.width + 15; 
    frame.size.height = frame.size.height + 15; 
    [self setFrame:frame]; 

    [self setAlpha:0.6]; 

}]; 

Draggable* sharedSingleton = [Draggable sharedManagerDraggable]; 

//nameOfTheMessage = sharedSingleton.namePassedToDraggable; 

NSLog(@"%@, %d", sharedSingleton.namePassedToDraggableArray, self.tag); 


} 

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 
// Move relative to the original touch point 
CGPoint pt = [[touches anyObject] locationInView:self]; 
CGRect frame = [self frame]; 
frame.origin.x += pt.x - startLocation.x; 
frame.origin.y += pt.y - startLocation.y; 
[self setFrame:frame]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

[[NSUserDefaults standardUserDefaults] synchronize]; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"MoveAllViewsDownForDrag" object:self userInfo:nil]; 

CGRect frame = [self frame]; 
if (frame.origin.y < 50) { 

    SaveEventsOrganizer* sharedSingletonOrganizer = [SaveEventsOrganizer sharedManagerSaveEventsOrganizer]; 
    Draggable* sharedSingletonDraggable = [Draggable sharedManagerDraggable]; 

    NSString *string = sharedSingletonDraggable.namePassedToDraggableArray[self.tag - 1]; 

    sharedSingletonOrganizer.NameOfEventPassedToOrganizerFromDraggable = string; 
    [sharedSingletonOrganizer addAnEvent]; 

    [self removeFromSuperview]; 

}else{ 

    [UIImageView animateWithDuration:0.5 animations:^(void) { 

     CGRect frame = [self frame]; 
     frame.size.width = frame.size.width - 15; 
     frame.size.height = frame.size.height - 15; 
     frame.origin.x = originXDraggable; 
     frame.origin.y = originYDraggable; 
     [self setFrame:frame]; 

     [self setAlpha:1.0]; 

    }]; 

    /*CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
    [anim setToValue:[NSNumber numberWithFloat:0.0f]]; 
    [anim setFromValue:[NSNumber numberWithDouble:M_PI/80]]; // rotation angle 
    [anim setDuration:0.1]; 
    [anim setRepeatCount:NSUIntegerMax]; 
    [anim setAutoreverses:YES]; 
    [self.layer addAnimation:anim forKey:@"iconShake"];*/ 

} 
} 

唯一的问题是,我不知道如何检测哪些按钮已被按下来移动它。有任何想法吗??

+0

当你说“按住他们”,你的意思是你希望用户必须长按按钮在它将变得可拖动之前? –

回答

2

您可以使用命中测试。请参阅我的书的例子:

http://www.apeth.com/iOSBook/ch18.html#_hit_testing

已经获得了你的背景来看...

CGPoint pt = [[touches anyObject] locationInView:self.view]; 

现在,您可以点击测试什么子视图(按钮),如果有的话,那个点是:

UIView* v = [self.view hitTest:pt withEvent:nil]; 

但是,我认为你这样做的方式总体上是非常复杂的。使用手势识别器可能会更加快乐。我在书中还描述了如何使用UIPanGestureRecognizer拖动对象:

http://www.apeth.com/iOSBook/ch18.html#_gesture_recognizers

+0

伟大的书,我会去的pangesture识别器 – Alessandro