CSS
我有一个带有表计划的数据库,每个表都有一个geometry属性,具有x和y值。在web浏览器中显示这些值时,呈现方式如下所示:
getStyles() {
const { x, y } = this.props.geometry || {};
return {
left: `${x}%`,
top: `${y}%`,
};
}显然,x和y是100%的百分比值。
iOS
我已经为每个表的视图(TableView)创建了一个UIScrollView和一个子类。当视图添加到scrollView中时,将调用TableView中的一个方法来更新表格位置,如下所示:
- (void)updateTablePosition:(Table *)table {
if (self.superview) {
float x_position = (self.superview.frame.size.width / 100) * table.position.x;
float y_position = (self.superview.frame.size.height / 100) * table.position.y;
[self setFrame:CGRectMake(x_position, y_position, self.frame.size.width, self.frame.size.height)];
}
}位置很完美!然而,我对每个TableView都有一个平移手势,允许我移动它们和改变位置,唯一的问题是我不知道如何将这个值转换回它在CSS中的值(百分比)。
编辑:删除了改变位置的代码,因为它是完全错误的。
- (void)tablePanAction:(UIPanGestureRecognizer *)sender {
UIScrollView *scrollView = (UIScrollView *)self.superview;
if (sender.state == UIGestureRecognizerStateBegan) {
[scrollView setScrollEnabled:NO];
}
CGPoint updatedLocation = [sender locationInView:scrollView];
self.center = updatedLocation;
if (sender.state == UIGestureRecognizerStateEnded) {
// from here, we should convert the updated location
// back to a relative percentage of the scrollView
}
}发布于 2019-09-17 21:32:26
事实证明..。答案简单得离谱,只需要一点思考。无论如何,我回答这个问题是为了以防任何人对数学没有完全的了解。
新的Table.geometry位置可以这样计算:
float new_css_x = (self.frame.origin.x / scrollView.frame.size.width) * 100;
float new_css_y = (self.frame.origin.y / scrollView.frame.size.height) * 100;https://stackoverflow.com/questions/57974568
复制相似问题