I have a UIButton and a UIView that belong to two different views hierarchies and I'm trying to detect a collision when I drag my button to the “viewTrashArea”.
The problem is that the frame of the button and the frame of the view are in different coordinates and therefore make the collision think that they are touching but they are far away from each others.
How can I detect the collision based on the global screen position?
Given
UIButton *button;
UIView *viewTrashArea;
This line would return true if they intersect:
CGRectIntersectsRect([button convertRect:button.bounds toView:viewTrashArea], viewTrashArea.bounds);
You'll need to convert the positions to a common coordinate system. Use the UIView methods convertRect:fromView: and convertRect:toView: for this purpose.
Related
Is there a way to determine how far a UIView has been rotated in Swift? For example, the UIView is rotated by the user using a rotation gesture recognizer, and then later, outside the gesture recognizer callback function, I want to determine how many degrees (or radians) the UIView was rotated.
I think it can be done using CGRectGetMinX, CGRectGetMaxX, CGRectGetMinY and CGRectGetMaxY but is there a simpler way to do it?
atan2(view.transform.b, view.transform.a) should do the trick if the UIView was not skewed.
I have multiple buttons in the shape of an octagon, and they are all right next to each other. Sharing edges. However, when i import these custom images for a button, the bounds of the custom shape is a square. Therefore, part of one of the octagon is overlapping the one right next to this. Not the actual octagon but the transform tool/modifer button bounds. Thus part of the button, though it is hidden, is overlapping another button. How do i modify the button to only take shape to the bounds of the custom shape?
subclass UIButton and override -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event. check if the point is inside your octagon shape and return YES in that case, otherwise NO. Then the buttons will technically still be overlapping but only consume touch events inside your shape.
My ImageView rotates but while it rotates it doesn't recognize touches on itself.
Do you think it's okay if I create a button via code that's over the ImageView that recognizes the touch?
When an animation is applied on any UIView or any subclass object of a UIView like UIImageView, UIButton etc then it does not detect touch events because when an animation is applied to a view, the animated property changes to its end value right away. what you are actually seeing on screen is the presentation layer of your views layer.
To answer your question, Yes, you can make a UIButton that covers up the area of the UIImageView to detect touch events on it. That sounds like the easiest to implement option in this case.
Apart from that, this link may also help you in the process. Hit testing animating layers
I have a UIImageView whose instance has a repeating pulse-like animation.
I want this UIImageView to move with the force of the accelerometer. To do this I have added another animation, this time a translation.
However, once this translation happens, it stops the pulse, even when its not animating.
As these two animations are separate, I cannot use the CGAffineTransformConcat method to combine both animations.
I have tried adding one to the UIView and one to the layer property of the same UIView but it does not work.
I have also tried adding the movement directly to the frame of the view, however, this just causes my view to disobey the constraint constraining it to the center x of the superview.
Does anyone use/know of a work around to this?
Thanks as always!
In my app the user can drag the top CALayer from a stack of CALayers.
When the users stops dragging the CALayer should return to its original position on top of the others CALayers. Unless the CALayer (after dragging) no longer overlaps with the other CALayers in which case it should move underneath the other CALayers.
Now, how can I detect whether two CALayers overlap?
CALayer's frame property is a CGRect. Fortunately for you, CGGeometry has the following method:
CGRectIntersectsRect
You'd use it like this:
if (CGRectIntersectsRect(myLayer1.frame,myLayer2.frame)) {
//code
}