缩放时保持UIButton相对
里面有一个UIImageView的UIScrollview,显示一个平面图。 我也有一个UIButton在滚动视图中充当标记/引脚。
我已经在图像上实现了缩放/双击,但是当图像滚动和/或缩放时,UIButton元素显然不会移动。
我已经尝试了下面的代码来尝试和缩放完成时重新放置button:
[myButton setFrame:CGRectMake( (myButton.frame.origin.x - myButton.frame.size.width / 2) * _scrollView.zoomScale, (myButton.frame.origin.y - myButton.frame.size.height / 2) * _scrollView.zoomScale, myButton.frame.size.width, myButton.frame.size.height)];
这将button移动到左上angular。
有没有人知道我应该怎么做,以保持button相对于图像(一个标记在Google地图上的相同方式)。
我最近回答了一个非常类似的问题 。 如果您不需要实时更新(只在缩放完成时更新),那么您应该可以使用此处列出的方法。
好吧,这是由以下解决:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:[UIImage imageNamed:@"Marker.png"] forState:UIControlStateNormal]; button.frame = CGRectMake(640.0, 230.0, 30.0, 46.0); [button addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchUpInside]; [self.imageView addSubview: button]; self.imageView.userInteractionEnabled = YES;
基本上通过直接添加button到imageView,然后启用userInteraction,我能够获得所需的所有function。