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