Moving a UIView along a CGPath according to touch position - iphone

I have a line graph I've drawn in Quartz, and a UIView 'bubble' that I'd like to ideally pop up when the user touches the single plot line, and moves their finger along it. The bubble displays some extra graph information.
I'd like to 'attach' the UIView to the CGPath plot, but I'm having trouble conceptually figuring out the best way to do this. I know you can animate a view along a CGPath, but this doesn't seem to work for me, because the user needs to 'scrub' along the graph themselves with their finger rather than any automatic animation.
Does anyone have any suggestions of a good approach?

Maybe you don't need to animate it. Touch events fly by pretty quickly -- maybe if you just move the view to the appropriate location relative to the touch without animation the move will be smooth enough. If that's not good enough, you'll have to animate along the graph segment from the current location of the view (see CALayer presentationLayer) to the desired location. The key is that you'll be animating every time you receive a touch event (and any previous animations would be cancelled).

Like Neil said, your best bet is to just move with the touch events, it will look smooth if all you're doing is moving a view. Use [aTouch locationInView:view] to get the touch's position, then find the closest point on the path (maybe use the x value and look up the y value on the path for the x).

Related

Modifying smooth curve with Anchor points

I wish to emulate this effect in Xcode with Swift. After some research I managed to find some articles about drawing smooth curves using a set of points .But I am still unclear about how I could to dynamically modify curves when the user touches/holds the screen.
Question :
I know how to make a smooth Bezier curve, but how can I add gesture recognizers such that by dragging the curve its shape changes.
I only need someone to point me in the right direction. Is there a guide or article in particular that could be useful?
Create transparent ControlPointView for every control point of the curve with a size of 50*50pt, so that users can easily tap them and drag.
Add a small image in the middle of every ControlPointView, so that users can see where the control point is located.
Add UIPanGestureRecognizer on every ControlPointView and handle it in view controller.
Use centers of control points to rebuild UIBezierPath every time gesture recognizer's state is changed.

Resizing curve by dragging control points

I'm trying to resize a drawn quadCurve by dragging one of its 3 control points so the curve can fit. What is the best approach to do this? letting you know that I'm using an imageView for drawing. Not using drawRect.
I know that I should detect if the touch is on the control points which is pretty easy but I don't know what to do after in my touchMoved and touchEnded methods.
Several things:
I would not use an image view for this. This is the kind of problem that drawRect: is for.
Don't use touchesMoved. Use a UIPanGestureRecognizer on the control points.
Make the control points subviews so you can attach gesture recognizers to them.
To work well, the control points typically need to have a pretty large hit area (larger than they are visually). You can do this pretty easily by making the control point views larger than what they draw (so if they're drawn as a 13 point circle, you put that in the middle of a 23 point view).
For an example of code that does all this see CurvyTextView.m. It doesn't do the last point (the control point views are too small to use well on a real device). Ignore all the text drawing code. You just care about updateControlPoints, addControlPoint:color:, initWithFrame:, pan:, and drawPath.

a touchable polygon on a view ?

I need to show a view on which I need to animate a polygon using its vertices. The polygon should be touchable, thus fire an event once touched, and I need to be able to move its vertices using some animation procedure, once it has fired that event.
I need to have three polygons like that to form a 3D Cube.
The darkened area is the view (actually an image) on which I have the cube.
There are two steps in the process: drawing and event handling.
Drawing can be done with Quartz2D, by implementing a drawRect in a view, calculating the coordinates of the cube on screen, followed by creating and drawing the path, which works fine for solidly filled shapes. The alternative method uses an OpenGL view where you specify triangles.
At the event handling end, you can implement onTouchesBegan: and friends to get the location of the interaction, and possibly hitTest: to allow other views below it to handle subsequent events. The next thing you will need to decide is how accurate you want to be - you can define a box that roughly matches the cube and test that for touches. Most people will want to touch it somewhere in the middle anyway. For accurate testing, you need the screen coordinates, and test each triangle in each polygon to see if it contains the location. Google turned up a nice explanation on the maths needed for that. In the OpenGL case, you'll have to manually repeat the calculations performed by OpenGL to find out where on screen your polygons have ended up.

Handling touch events on UIImageViews

I have a UIImageView, above which I have multiple markers. All the markers are movable, i.e. when I touch a marker, I can move it all around the screen.
Problem: if I touch a marker, and I begin to move it to another place, if I cross another marker, the old marker is left in place of the new one, and I continue to move the new one. And I want to evade this somehow.
Thanks for the answer.
What are your markers? If they are UIView instances then I would suggest watching for touch events on them instead of the image view and then deal with the Z order while dragging.
I would also pay attention to touch up vs. touch moved to help with the issue of your stops and starts.
If they are not UIView instances then the issue sounds like it is with your touch down vs moved. i.e. you should keep track of what marker was touched on the down event and then ignore any events that hit another marker that are not down events until you get an up.
If these suggestions are not helpful then describing how you built your view structure would help.
Z-Order
When you receive a touch down event on a marker I would move it to the top of the z order of views within the parent. Then will allow it to visually slide over the other markers.

How to make swoosh graphic/sound effect?

I have a UIImageView that can be dragged around with a finger. It is sized to an image in it, such as a ball or square. If the user accelerates very quicker, I'd like to trail the object with a blur of itself to give the appearance of going fast. Like what happens with warp speed. I also have a swoosh sound. I'd like the sound to last only as long as the user is accelerating.
How can I accomplish these effects?
Well, the sound is completely out of my skill-set - OpenGL has no support for sounds, you'd have to look into OpenAL or whatever equivalents exist on the iPhone.
A simple way to implement the blur is to keep an array of the last few points that the object has been. Whenever the UIImageView is moved (I'm not sure if there's an event, or if you have to check every frame), you push the new locations onto the array, deleting the oldest one.
Whenever it comes time to render the object, look at the last few points and calculate the distance between current position and the latest item in the array. If it's above a certain distance, draw a blur at the array location and check the next two points. Continue until the distance is below a certain amount.
Hope this helps!