Rotate image with one finger [closed] - ios

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to make a volume knob like this:
https://s-media-cache-ak0.pinimg.com/736x/29/b5/85/29b58559e3d8a09dcd7e1a47c700ca76.jpg
But I only want to use one finger, UIGestureRotate is great but only supports rotation with two fingers and I don't seem to find the solution for UIPan​Gesture​Recognizer to rotate as I want it to? Is there a simple way to simulate the second finger in rotategesture or do I have to calculate pan?

If you ignore gesture recognizers and override touches handlers on a view, you can use math to do this. With a little trig, take an arbitrary anchor point along an axis (say 9 o'clock on the knob), and your touches' locations on the plane with (0, 0) at the center of your knob. Say your anchor point is at (-3, 0) and your touch is at (-1, 1) for the sake of simplicity. Take the arctan of the change in y over the change in x, in this case arctan of 1/2, which is roughly 0.464 radians, or roughly 26.585 degrees. You would rotate your knob 26.6 degrees clockwise (in this case) from your anchor point to have your knob follow your touch. In Swift:
//Assume you have an anchor point "anchor" and a touch location "loc"
let radiansToRotate = atan2(loc.y - anchor.y, loc.x - anchor.x)
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = radiansToRotate
rotateAnimation.duration = 1.0 //the longer the duration, the "heavier" your knob
That rotate animation will make the knob "follow" your finger, the desired effect. For completeness' sake, in a very basic form, atan2 is a math function that protects you from undefined arctan values. You'll have to worry about rotation direction, possibly, if your values fluctuate wildly, so you may consider using a key frame animation for this (I'll leave this to you).

Related

3D animate text in iOS / Swift as if it were written on an invisible rotating toilet paper roll [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Whether it is fake 3D (illusion to the same effect) or actual 3D, I would like to animate text and images as if they were printed on an invisible rotating toilet paper roll, the same way that you would rotate a toilet roll around its holder. Another analogy would be a poker machine - so that the old symbols slide out, and the new ones slide in.
I have managed to do this with fake 3D (2D animation only to create an illusion). But I am not happy with it. Also, one of the animated images must be spinning (it's a custom spinner made by our graphical designers which must be spinning in its own coordinate space), and my way is incompatible with that animation happening at the same time. It glitches out when they are combined. So for the poker machine analogy, imagine the fruit symbol is spinning in place.
Thanks
Where the variable myScale is the scale of the animation of your choosing, I came up with:
viewSlideInFromBottom.layer.transform = CATransform3DConcat(CATransform3DMakeTranslation(0, 0, -myScale), CATransform3DMakeRotation(CGFloat.pi / 2, 1, 0, 0))
viewSlideOutToTop.layer.transform = CATransform3DMakeTranslation(0, 0, -myScale)
let animation = UIViewPropertyAnimator(duration: 1.0,
curve: .easeIn,
animations: {
self.viewSlideInFromBottom.layer.transform = CATransform3DMakeTranslation(0, 0, -self.myScale)
self.viewSlideOutToTop.layer.transform = CATransform3DConcat(CATransform3DMakeTranslation(0, 0, -self.myScale), CATransform3DMakeRotation(-CGFloat.pi / 2, 1, 0, 0))
}
)
animation.startAnimation()
viewSlideInFromBottom slides in from the bottom and viewSlideOutToTop slides out of the top.

How can I throw in sprites from both sides of the screen in Swift? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to apply a sort of impulse onto a sprite and have it thrown onto the screen from the side. However, when it is after the screen I want the impulse to stop and for the regular physics to be applied to it, so I would be able to drag it around. Its difficult to explain but if anyone can help out it would be very much appreciated. Thanks!
Apple docs show that a we can influence the physicsBody of a sprite with these instance methods:
- applyForce:
- applyTorque:
- applyForce:atPoint:
- applyImpulse:
- applyAngularImpulse:
- applyImpulse:atPoint:
As for "throwing it on the screen from the side", you can have the sprite spawn outside your current skViews bounds, and have the force be applied to it accordingly (if the sprite is located outside the north side of the screen, you'll want to applyImpulse downward).
With respect to "regular physics" upon entrance of the skView, you can have a check in update:(CFTimeInterval)currentTime for those types of sprites entering the skViews frame OR define a protocol within your sprite's class, and have your SKScene subclass conform to that protocol; then have your sprite fire a method when entering a certain frame (skView.frame), which your SKScene will be able to respond to. You can then apply counter forces or impulses to that sprite.

Panning an image/images in Swift using an iPad compass [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have four images; each is 350 pixels wide; I have the text North on the first, East on the second, South on the third and West on the fourth. I put them into a scrollView in a single image and I link said scrollView to the direction I am pointing my iPad. Now as I pan around I want the scroll to pan around the images, so if I am looking north and turn east I see north slide of the screen and east onto it.
Now I tried to do this using the compass, and it works reasonably well, but how to code crossing the boundary between 0 degrees and 360 degrees is a challenge. I tried using yaw so that I am looking at two 180 degree circles, but working out how to make that work has evaded me too. I google this and find quaternion and euler angles, but nothing that makes enough sense to me...
Need some direction/help.
You don't really need a scroll view for this, since you are controlling the scroll offset by device heading rather than by letting the user swipe the screen. So you could just use a plain old UIView as the parent of the image views. You would just update view.bounds.origin instead of scrollView.contentOffset to “scroll”. (Under the hood, the scroll view's contentOffset is the same as its bounds.origin.)
Anyway, lay out your images like this:
+-------+-------+-------+-------+-------+
| North | East | South | West | North |
+-------+-------+-------+-------+-------+
Note that the North image is used twice.
When you get a new heading, update the offset of the scroll view like this:
let range = CGFloat(350 * 4)
let offset = range * (CGFloat)direction / 360
scrollView.contentOffset = CGPointMake(offset, 0)

Limit view movement to a circular area [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to setup a custom color picker. I have setup a circular image with a picker bug. But, I want the bug to only move over the image (there are no colours outside the image), or, another way, only in the circle of the image size.
How can I limit the bug position?
I understand that you want to keep your picker inside the circle image.
To do that just simply grab center point (p1) of your circle image and center point (p2) of currnet position of the picker. Calculate distans between both:
float distance = sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
And if the distance is more that circle radius stop move your picker:
if(distance <= radius)
return YES;// Let the picker move, it's inside the circle image
else
return NO; // Picker outside the circle image
Hope this is what you are about.

XNA Changing Draw When player uses keyboard [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Would someone be able to give me an example how to change the texture drawn when the user pressed the keyboard ?
Basically I have 5 sprite images stood still, up, down, left, right
My first attempt was to make boolean conditions in the update method such as if keys.left is pressed the rectangle moves left and for the draw method to draw the character moving left, my problem is the texture of him stood still doesnt disappear and overlaps the same when moving in different directions.
I've tried else statements etc but I'm stuck on basic movements.
So this is very pseudo code, but this is a basic approach how to do that
First we create class which contains all our informations about the player. You can add Health, Score etc aswell.
class Player
{
Texture2D Sprite;
Vector2 Position;
Vector2 Velocity;
static const float PlayerSpeed = 5;
}
Important here is the Position (top left of the sprite), the Velocity(the amount of change every second) and the sprite which is just our texture we want to use. Of course it would be better to use only one player texture and modify the source rect accordingly.
Now our input handling.
void OnKeyboard(GameTime aGameTime, KeyArg aKey)
{
if(aKey == Keys.Left)
{
mPlayer.Velocity = new Vector2(-Player.PlayerSpeed, 0);
mPlayer.Sprite = TextureManager.GetTexture("left_player");
}
else if(aKey == Keys.Right)
{
mPlayer.Velocity = new Vector2(Player.PlayerSpeed, 0);
mPlayer.Sprite = TextureManager.GetTexture("right_player");
}
mPlayer.Position += aGameTime.EllapsedMiliseconds * mPlayer.Velocity;
}
Here we just check which key was pressed, modify the velocity and change the current sprite for our player. The final line is the most important one, this modifies the players position using the velocity modified by the elapsed time of the frame. That way you have a steady movement instead despite any frame rate inconsistencies.
void Render()
{
Sprite.Draw(mPlayer.Sprite, mPlayer.Position);
}
Finally the rendering, how to render a sprite should be clear, here you just use the set sprite and the position.
There is a lot of room for improvements, for example minimizing texture switches, handling sprites with alpha, and the most important one proper handling of the keyboard. You need to steadily adjust the position, but depending on how you implement it, the movement might be bound to the key repeat rate, which may not be desired.

Resources