我有一个UITableView。其中大约有5个单元格/行。
在每个单元格中都有一个按钮。当用户点击按钮时,我需要在其上方显示动画视图。 (此视图应设置为动画并从底部显示)
我的代码: 的的cellForRowAtIndexPath
[cell.button addTarget:self action:@selector(buttonclick:) forControlEvents:UIControlEventTouchUpInside];
- (void) buttonclick:(id) sender {
NewView *newView = [[NewView alloc] init];
newView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
[self.view addSubview: newView];
在自定义视图类
中-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[[NSBundle mainBundle] loadNibNamed:@"myview" owner:self options:nil];
self.bounds=self.myview.bounds;
[self addSubview:self. myview];
}
return self;
}
答案 0 :(得分:0)
如果NewView是重新实现drawRect
的自定义UIView,那么您需要调用setNeedsDisplay
请参阅此answer
答案 1 :(得分:0)
所有
的解决方案- (void) buttonclick:(id) sender {
UITableViewCell *cell=(UITableViewCell *)[sender superview];
NewView *newView = [[NewView alloc] initWithFrame:cell.bounds];
[self.view addSubview: newView];
//Position the view to the bottom of the view.
CGRect rect=newView.frame;
rect.origin.y=self.view.frame.size.height;
newView.frame=rect;
//New Position of y to animate from bottom
rect.origin.y=rect.origin.y-rect.size.height;
[UIView animateWithDuration:.2 animations:^{
newView.frame=rect;
}];
}
您的代码中存在错误。
当你在那里加载nib时,你没有调用 initWithFrame ,所以它永远不会被调用。
<强>更新强>
您错过了自定义类代码中的一个重要点
self.myview=[[[NSBundle mainBundle] loadNibNamed:@"myview" owner:self options:nil] lastObject];
希望它有所帮助。
干杯。
答案 2 :(得分:0)
- (void) buttonclick:(id) sender
{
UIButton *button = (UIButton *) sender;
UIView *senderSuperView = [button superView];// access cell
NewView *newView = [[NewView alloc] init];
[newView setFrame: senderSuperView.frame];
CGAffineTransform newViewActualTransform = newView.transform;
newView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
[self.view addSubview: newView];
newView.transform = CGAffineTransformScale(newViewActualTransform, 0.0, 0.0);
[UIView animateWithDuration:1.0f
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
newView.transform = CGAffineTransformScale(newViewActualTransform, 1.0, 1.0);
}
completion:^(BOOL finished){
}
];
}