【问题标题】:Programmatically added subview doesn't interact with other views, but xib-added view worked fine以编程方式添加的子视图不与其他视图交互,但 xib 添加的视图工作正常
【发布时间】:2013-01-23 05:08:03
【问题描述】:

harlanhaskins 写道:

我在 viewDidLoad 中以编程方式添加了一个名为 userSlider 的视图:

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

还有一个叫做gameTableView的tableview。

我最初将 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 突然不会改变,除非我在移动它之后在该区域内点击。真的很奇怪。

有什么想法吗?

【问题讨论】:

  • 如果将 viewDidLoad 中的这 4 行代码移动到 viewWillAppear 会发生什么?
  • 没什么。不过有趣的是,当视图没有被添加为子视图时,tableView 调整得很好......

标签: ios uiview touch touchesmoved


【解决方案1】:

如果你的 userSlider 的框架是

CGRectMake(0, -120, 320, 155)   

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

CGRectMake(0, 0, 320, 155)  

试一试。

【讨论】:

  • 它可以很好地处理事件,并且 userSlider 会随着我的点击而移动,就像我想要的那样。但是 tableView 不会移动,除非我在 120 到 155 之间点击 它被拉下...
  • @HarlanHaskins 你希望桌子只有在 120 到 155 之间被点击时才移动,对吧?
  • 让我进一步解释一下。 imgur.com/a/OkeSh 视图从 0 到 120(垂直)隐藏(相对于自身)。当你把它拉下来时,它只会移动中心。它还应该调整 tableView 的大小和原点。
猜你喜欢
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 2014-08-24
  • 2013-12-03
  • 1970-01-01
  • 2015-04-01
相关资源
最近更新 更多