我只需要在子视图中拖放 uibutton。实际上我正在主视图中查看子视图。UIbutton 添加到子视图中。当我拖动按钮时,它会在两个视图上移动,但我只需要在子视图中拖动按钮。谁能帮助我。提前致谢。
- (void) drag
{
//subview
UIView *viewscramble = [[UIView alloc] initWithFrame:CGRectMake(0, 90, 320, 50)];
[viewscramble setBackgroundColor:[UIColor redColor]];
[self.view addSubview:viewscramble];
//button
UIButton *btnscramble = [[UIButton alloc] initWithFrame:CGRectMake(5, 0, 50, 50)];
btnscramble.titleLabel.text = @"A";
[btnscramble setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnscramble addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[btnscramble setBackgroundImage:[UIImage imageNamed:@"box.png"] forState:UIControlStateNormal];
//add button`
[viewscramble addSubview:btnscramble];
[btnscramble release];
}
- (IBAction)imageMoved:(id)sender withEvent:(UIEvent *) event
{
CGPoint point = [[[event allTouches]anyObject] locationInView:viewscramble];
UIControl *control = sender;
control.center = point;
}