2013-03-11 47 views
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 

回答

1

試試這個:

basketView.userInteractionEnabled = YES; 

Documentation says that:

新的圖像視對象被默認配置爲忽略用戶的事件。如果要處理UIImageView的自定義子類中的事件,則必須在初始化對象後明確將userInteractionEnabled屬性的值更改爲YES。

+1

工作!感謝您的解釋! – user1824518 2013-03-11 06:44:48