哈兰哈斯金斯写道:
我在 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 突然不会改变,除非我在移动它之后在该区域内点击。这真的很奇怪。
有任何想法吗?