以编程方式添加的子视图不与其他视图交互,但添加了xib的视图工作正常

时间:2013-01-23 05:08:03

标签: ios uiview touch touchesmoved

harlanhaskins写道:

我在viewDidLoad中添加了一个名为userSlider programmaticaly的视图:

 self.userSlider = [[UserSliderView alloc] initWithFrame:CGRectMake(0, -120, 320, 155)];
     [self.userSlider setUser:user];
     [self.userSlider configureView];
     [self.view addSubview:self.userSlider];

一个名为gameTableView的桌面视图。

我最初将userSlider作为自定义视图添加到xib中(因为UserSlider有自己的xib),当我实现这些触摸方法时:

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = (UITouch*)[touches anyObject];

start = [touch locationInView:self.view].y;
 }



 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

if(start < 0)

{

    return;

}

UITouch *touch = (UITouch *)[touches anyObject];

CGFloat now = [touch locationInView:self.view].y;

CGFloat diff = now - start;

sliderDirectionIsUp = diff < 0; //sliderDirectionIsUp is a BOOL

if ((self.userSlider.frame.origin.y == 0) && ((now < 120) || (now > 155))) {

    return;

}

float newCenterY = self.userSlider.center.y + diff;

if (newCenterY < roundf(self.userSlider.frame.size.height / 2)) {

    self.userSlider.center = CGPointMake(self.userSlider.center.x, newCenterY);

    CGRect tableViewFrame = self.gameTableView.frame;

    tableViewFrame.origin.y += diff;

    tableViewFrame.size.height -= diff;

    [self.gameTableView setFrame:tableViewFrame];

}

start = now;

 }



 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

 {

CGRect tableViewFrame = self.gameTableView.frame;

if (sliderDirectionIsUp)

{

    tableViewFrame.origin.y = 28;

    tableViewFrame.size.height = (self.view.frame.size.height - tableViewFrame.origin.y);

    //animate userSlider out of visible area

    [UIView animateWithDuration:.3 animations:^{

        self.userSlider.center = CGPointMake(self.userSlider.center.x, -roundf(self.userSlider.bounds.size.height/2.) + 35);

        [self.gameTableView setFrame:tableViewFrame];

    }];

}

else if(start >= 0)

{

    tableViewFrame.origin.y = (self.userSlider.frame.size.height - 7);

    tableViewFrame.size.height = (self.view.frame.size.height - tableViewFrame.origin.y);

    //animate userSlider with top to mainviews top

    [UIView animateWithDuration:.3 animations:^{

        self.userSlider.center = CGPointMake(self.userSlider.center.x, roundf(self.userSlider.bounds.size.height/2.) - 0.5);

        [self.gameTableView setFrame:tableViewFrame];

    }];

}

 }

然后两个视图都会移动我想要的方式。

但现在当我以编程方式添加它时,突然tableView不会改变,除非我在移动它之后点击该区域。这真的很奇怪。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果您的userSlider的框架是

CGRectMake(0, -120, 320, 155)   

我怀疑你的userSlider是否可以接收发件人事件。因为如果UIControl的帧不在superView中,它可能不会收到发件人事件(外部部分)。您可以设置框架

CGRectMake(0, 0, 320, 155)  

试一试。