みかづきブログ その3

本ブログは更新を終了しました。通算140万ユーザーの方に観覧頂くことができました。長い間、ありがとうございました。

👆

引越し先はこちらです!

UIImageViewにタッチイベントを設定してみよう。

いろいろ方法はあるようですが、今回つかった方法をメモしておきます。

viewController.xib

対象となる ImageView の User Interaction Enabled にチェックを入れる。

f:id:kimizuka:20140206095554p:plain

viewController.xib と viewController.m のあいだ

対象となるImageView を viewController.xib から controlを押しながら viewController.m に持ってくる
今回はimageViewと名付けてみました。

@property (nonatomic, weak) IBOutlet UIImageView *imageView;

viewController.m

タッチの開始、移動、終了、キャンセルをそれぞれ実装します。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if([event touchesForView:_imageView] != NULL)
    {
        NSLog(@"さわりはじめたなう。");
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if([event touchesForView:_imageView] != NULL)
    {
        NSLog(@"指移動なう。");
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if([event touchesForView:_imageView] != NULL)
    {
        NSLog(@"さわり終わったなう。");
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    if([event touchesForView:_imageView] != NULL)
    {
        NSLog(@"タッチキャンセルなう。");
    }
}

今回は以上です。