Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 实现触摸,用手指拖动各种图像_Objective C - Fatal编程技术网

Objective c 实现触摸,用手指拖动各种图像

Objective c 实现触摸,用手指拖动各种图像,objective-c,Objective C,我正在尝试使用触摸来拖动各种图像。到目前为止,我试图让它只与一个图像一起工作,但它不工作(即,我不能用手指移动图像)。我做错了什么 我有几个代码如下的文件: DragView.h @interface DragView : UIImageView { } @end #import <UIKit/UIKit.h> #import "DragView.h" @interface ViewController : UIViewController { } @property (non

我正在尝试使用触摸来拖动各种图像。到目前为止,我试图让它只与一个图像一起工作,但它不工作(即,我不能用手指移动图像)。我做错了什么

我有几个代码如下的文件:

DragView.h

@interface DragView : UIImageView {
}
@end
#import <UIKit/UIKit.h>
#import "DragView.h"

@interface ViewController : UIViewController {
}

@property (nonatomic, strong) DragView *basketView;

@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
 #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
ViewController.h

@interface DragView : UIImageView {
}
@end
#import <UIKit/UIKit.h>
#import "DragView.h"

@interface ViewController : UIViewController {
}

@property (nonatomic, strong) DragView *basketView;

@end
试试这个:

basketView.userInteractionEnabled = YES;

默认情况下,新图像视图对象配置为忽略用户事件。如果要处理UIImageView的自定义子类中的事件,必须在初始化对象后将userInteractionEnabled属性的值显式更改为YES

试试这个:

basketView.userInteractionEnabled = YES;

默认情况下,新图像视图对象配置为忽略用户事件。如果要处理UIImageView的自定义子类中的事件,必须在初始化对象后将userInteractionEnabled属性的值显式更改为YES