2013-04-13 253 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