0

画面に多くの UIbuttons があり、ユーザーがボタンを押したままドラッグしてボタンを移動できるようにしたい。私はこのコードを思いつきました:

- (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"];*/

}
}

唯一の問題は、どのボタンが押されて移動したかを検出する方法がわからないことです。何か案は??

4

1 に答える 1

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

于 2013-04-13T19:18:03.670 に答える