1

タッチを使用してさまざまな画像をドラッグできるようにしようとしています。これまでのところ、1 つの画像だけで機能するようにしようとしていますが、機能していません (つまり、指で画像を移動できません)。私は何を間違っていますか?

次のコードを含むファイルがいくつかあります。

DragView.h

@interface DragView : UIImageView {
}
@end

DragView.m

#include "DragView.h"
    @implementation DragView

    - (void)touchesMoved:(NSSet *)set withEvent:(UIEvent *)event {
        CGPoint p = [[set anyObject] locationInView:self.superview];
        self.center = p;
    }

    @end

ViewController.h

#import <UIKit/UIKit.h>
#import "DragView.h"

@interface ViewController : UIViewController {
}

@property (nonatomic, strong) DragView *basketView;

@end

ViewController.m

 #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    @synthesize basketView;

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        basketView = [[DragView alloc]
                      initWithImage:[UIImage imageNamed:@"basket.png"]];
        basketView.frame = CGRectMake(140, 340.2, 60, 30);
        [self.view addSubview:basketView];
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    @end
4

1 に答える 1

1

これを試して:

basketView.userInteractionEnabled = YES;

ドキュメンテーションは次のように述べています。

新しいイメージ ビュー オブジェクトは、デフォルトでユーザー イベントを無視するように構成されています。UIImageView のカスタム サブクラスでイベントを処理する場合は、オブジェクトの初期化後に userInteractionEnabled プロパティの値を明示的に YES に変更する必要があります。

于 2013-03-11T06:30:58.833 に答える