我正在尝试从笔尖加载视图并将其添加到主视图中。这是我的代码。
我将笔尖装入@interface AddTaskView:UIView
的initWithFrame
:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(@"init with frame");
// Initialization code
NSArray *addTaskArrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"AddTaskView"
owner:self
options:nil];
self=[addTaskArrayOfViews objectAtIndex:0];
}
return self;
}
之后,我初始化视图并将其添加到主视图中:
self.addTaskView=[[AddTaskView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:self.addTaskView];
一切正常,但 AddTaskView 不会收到触摸事件。我有三个按钮,当我尝试按下它们时,它们没有收到操作。我做了"AddTaskView"类型的笔尖,但没有任何运气。我还查看了子视图,"AddTaskView"是顶部视图。
我在 AddTaskView 中添加了此代码,以查看我是否得到触摸但没有响应:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touch");
}
如何获得响应触摸事件的机会?
我认为
您必须注册一个触摸调度程序,然后尝试获得触摸......以下是该代码
-(void)registerWithTouchDispatcher{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0swallowsTouches:YES];}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
return YES;}
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
touchLocation = [touch locationInView:[touch view]];
NSLog(@"Touch is working");}
[UIView beginAnimations:@"MoveAndStrech" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView commitAnimations];