lud*_*udo 1 iphone objective-c ipad
我有一个我在UIImageView上添加的按钮.使用用户触摸屏幕时UIImageView将旋转的方法,我想知道旋转完成后是否有办法获取按钮的新位置.
现在我用这种方法一直得到原始位置:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Xposition : %f", myButton.frame.origin.x);
NSLog(@"Yposition : %f", myButton.frame.origin.y);
}
Run Code Online (Sandbox Code Playgroud)
谢谢,
这是一个棘手的问题.参考有关frame属性的UIView文档,它指出:
警告:如果transform属性不是identity变换,则此属性的值未定义,因此应忽略.
所以诀窍是寻找一种解决方法,这取决于你究竟需要什么.如果您只需要近似值,或者您的旋转始终是90度的倍数,则CGRectApplyAffineTransform()函数可能运行得很好.将它传递给感兴趣的UIButton的(未转换的)帧,以及按钮的当前变换,它将为您提供转换后的矩形.请注意,由于rect被定义为原点,宽度和高度,因此无法定义边长与屏幕边缘不平行的矩形.在它不平行的情况下,它将返回旋转的rect的最小可能的边界矩形.
现在,如果您需要知道一个或所有转换点的确切坐标,我之前已编写代码来计算它们,但它涉及的更多一些:
- (void)computeCornersOfTransformedView:(UIView*)transformedView relativeToView:(UIView*)parentView {
/* Computes the coordinates of each corner of transformedView in the coordinate system
* of parentView. Each is corner represented by an independent CGPoint. Doesn't do anything
* with the transformed points because this is, after all, just an example.
*/
// Cache the current transform, and restore the view to a normal position and size.
CGAffineTransform cachedTransform = transformedView.transform;
transformedView.transform = CGAffineTransformIdentity;
// Note each of the (untransformed) points of interest.
CGPoint topLeft = CGPointMake(0, 0);
CGPoint bottomLeft = CGPointMake(0, transformedView.frame.size.height);
CGPoint bottomRight = CGPointMake(transformedView.frame.size.width, transformedView.frame.size.height);
CGPoint topRight = CGPointMake(transformedView.frame.size.width, 0);
// Re-apply the transform.
transformedView.transform = cachedTransform;
// Use handy built-in UIView methods to convert the points.
topLeft = [transformedView convertPoint:topLeft toView:parentView];
bottomLeft = [transformedView convertPoint:bottomLeft toView:parentView];
bottomRight = [transformedView convertPoint:bottomRight toView:parentView];
topRight = [transformedView convertPoint:topRight toView:parentView];
// Do something with the newly acquired points.
}
Run Code Online (Sandbox Code Playgroud)
请原谅代码中的任何小错误,我在浏览器中写了它.不是最有帮助的IDE ...
| 归档时间: |
|
| 查看次数: |
1075 次 |
| 最近记录: |