1

我试图能够使用触摸来拖动各种图像。到目前为止,我试图让它只使用 1 张图像,但它不起作用(即我无法用手指移动图像)。我究竟做错了什么?

我有几个包含以下代码的文件:

拖动视图.h

@interface DragView : UIImageView {
}
@end

拖动视图.m

#include "DragView.h"
    @implementation DragView

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

    @end

视图控制器.h

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

@interface ViewController : UIViewController {
}

@property (nonatomic, strong) DragView *basketView;

@end

视图控制器.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 回答