当在 uiview 上使用捏合手势时,它的边界会改变吗?
我正在使用 UIPinchGestureRecognizer 来扩展/缩小 uiview。
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleElement:)];
[pinchGesture setDelegate:self];
[element addGestureRecognizer:pinchGesture];
[pinchGesture release];
//缩放元素方法
- (void)scaleElement:(UIPinchGestureRecognizer *)gestureRecognizer {
UIView *element = [gestureRecognizer view];
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan){
lastTouchPosition = [gestureRecognizer locationInView:element];
}
else if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged){
CGPoint currentTouchPosition = [gestureRecognizer locationInView:element];
CGPoint deltaMove = [self calculatePointDistancewithPoint1:currentTouchPosition andPoint2:lastTouchPosition];
float distance = sqrt(deltaMove.x*deltaMove.x + deltaMove.y*deltaMove.y);
float hScale = 1 - deltaMove.x/distance * (1-gestureRecognizer.scale);
float vScale = 1 - deltaMove.y/distance * (1-gestureRecognizer.scale);
if (distance == 0) {
hScale = 1;
vScale = 1;
}
element.transform = CGAffineTransformScale([element transform], hScale, vScale);
CGAffineTransform transform = CGAffineTransformMakeScale(hScale, vScale);
element.bounds = CGRectApplyAffineTransform(element.bounds, transform);
[gestureRecognizer setScale:1];
lastTouchPosition = currentTouchPosition;
}
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {
NSLog(@"scaling over");
NSLog(@"bounds = %@",NSStringFromCGRect(element.bounds));
NSLog(@"frame = %@", NSStringFromCGRect(element.frame));
}
NSLog(@"scalePiece exit");
}
//adjustAnchorPointForGestureRecognizer method
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
UIView *elementToAdjust = gestureRecognizer.view;
[self.view bringSubviewToFront:gestureRecognizer.view];
CGPoint locationInView = [gestureRecognizer locationInView:elementToAdjust];
CGPoint locationInSuperview = [gestureRecognizer locationInView:elementToAdjust.superview];
elementToAdjust.layer.anchorPoint = CGPointMake(locationInView.x / elementToAdjust.bounds.size.width, locationInView.y / elementToAdjust.bounds.size.height);
elementToAdjust.center = locationInSuperview;
}
}
控制台打印:
bounds = {{0, 0}, {178.405, 179.018}}
frame = {{300.642, 566.184}, {192.899, 194.227}}
为什么框架改变时边界没有调整? 它与自动调整蒙版大小有什么关系,因为我有此视图的子视图?
I am using the UIPinchGestureRecognizer to expand/reduce a uiview.
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleElement:)];
[pinchGesture setDelegate:self];
[element addGestureRecognizer:pinchGesture];
[pinchGesture release];
//Scale element method
- (void)scaleElement:(UIPinchGestureRecognizer *)gestureRecognizer {
UIView *element = [gestureRecognizer view];
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan){
lastTouchPosition = [gestureRecognizer locationInView:element];
}
else if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged){
CGPoint currentTouchPosition = [gestureRecognizer locationInView:element];
CGPoint deltaMove = [self calculatePointDistancewithPoint1:currentTouchPosition andPoint2:lastTouchPosition];
float distance = sqrt(deltaMove.x*deltaMove.x + deltaMove.y*deltaMove.y);
float hScale = 1 - deltaMove.x/distance * (1-gestureRecognizer.scale);
float vScale = 1 - deltaMove.y/distance * (1-gestureRecognizer.scale);
if (distance == 0) {
hScale = 1;
vScale = 1;
}
element.transform = CGAffineTransformScale([element transform], hScale, vScale);
CGAffineTransform transform = CGAffineTransformMakeScale(hScale, vScale);
element.bounds = CGRectApplyAffineTransform(element.bounds, transform);
[gestureRecognizer setScale:1];
lastTouchPosition = currentTouchPosition;
}
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {
NSLog(@"scaling over");
NSLog(@"bounds = %@",NSStringFromCGRect(element.bounds));
NSLog(@"frame = %@", NSStringFromCGRect(element.frame));
}
NSLog(@"scalePiece exit");
}
//adjustAnchorPointForGestureRecognizer method
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
UIView *elementToAdjust = gestureRecognizer.view;
[self.view bringSubviewToFront:gestureRecognizer.view];
CGPoint locationInView = [gestureRecognizer locationInView:elementToAdjust];
CGPoint locationInSuperview = [gestureRecognizer locationInView:elementToAdjust.superview];
elementToAdjust.layer.anchorPoint = CGPointMake(locationInView.x / elementToAdjust.bounds.size.width, locationInView.y / elementToAdjust.bounds.size.height);
elementToAdjust.center = locationInSuperview;
}
}
Console print:
bounds = {{0, 0}, {178.405, 179.018}}
frame = {{300.642, 566.184}, {192.899, 194.227}}
Why isn't the bounds adjusting when the frame is changing ?
Does it have anything to do with auto resizing masks as I have subviews to this view ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与
UIPinchGestureRecognizer
无关。这与您设置transform
以执行缩放有关。更改变换
不会更改边界
。它只是更改此视图的坐标空间 (bounds
) 映射到父视图的坐标空间 (frame
) 的方式。如果您需要bounds
来匹配frame
,则必须直接更改其中一个,而不是使用transform
。也就是说,您通常应该使用transform
因为它要快得多。编辑
如果您不想缩放,而是想调整视图大小,请调用
setBounds:
。您可以通过将转换应用于bounds
而不是元素来找到新的bounds
。This has nothing to do with
UIPinchGestureRecognizer
. This has everything to do with your setting thetransform
to perform scaling. Changing thetransform
does not changebounds
. It just changes how this view's coordinate space (bounds
) maps to the superview's coordinate space (frame
). If you needbounds
to matchframe
, you have to change one of them directly, not usetransform
. That said, you should generally usetransform
because it's much faster.EDIT
If you don't mean to scale, but rather mean to resize the view, call
setBounds:
. You can find the newbounds
by applying the transform to thebounds
rather than the element.