How to make a line draw between two points as you are trying to set the second / end point of the line (like drawing a line in MSPAINT!) - iphone

I was wondering if anyone would be able to tell me how you would go about this?
e.g.
- User pushes (+) Add Button
- Image appears to let user set start point of line
- After user sets the first point a second image appears and a line is drawn between them and animated between them as user moves the second image into place.
You could see this sort of action in just about any graphics drawing program where a user is required to draw a line; except to help the user know where he is drawing I am including a start/end point image to drag about.
I can draw a line no problems. I am perplexed a little by this as the two images I generate for the user to drag around and set as start/end point are both instances of a class I created (draw2d) that subclasses UIImageView (so I can drag it about).
So at present I am adding two UIImageViews to my UIViewController in order to drag them about - you can't draw a line between two UIImageViews as such as the only drawing I know how to do is IN the UIImageView.
Do I need to nest these two images in a parent-style UIImageView so I can draw between them?
Any assistance is always greatly appreciated.

If I understand correctly, you want to draw the line while the user drags their finger around to set the endpoint?
Can't you just track the dragging positions and re-draw the line each time it changes, using the last dragged position as the 'temporary end point'?

Do I need to nest these two images in
a parent-style UIImageView so I can
draw between them?
Yes.

Related

Create a UIScrollView that can be embedded into another view

I'm trying to build a custom, reusable UIScrollView that can be added to multiple views. The scroll view is going to be a weight picker. For the life of me, I can't find a decent example for how to implement this neatly or cleanly.
I would love for someone to point me to an existing library or tutorial that shows me how to do this. I've hacked apart a few examples, but so far, nothing is very good or reusable. Please help!!
For what it's worth, I have an image that individual ticks for the weight. So I can select to the tenth of the number (e.g. 160.4). The image has the first tick bold and larger than the remaining 9. I'd like to have the weight/number centered over the large tick. I'll update the points to my label/datasource after scrolling stops.
UPDATE
I need to make this. I have the custom font, background, and ticker image.
I would not do this through an UIScrollView. I think it would be more complex and you would certainly end up having issues when trying to add you custom picker into another scroll view.
What I would do is:
building the picker view by means of a series of CALayers, each one representing a "building block" of your picker view; see attached image:
each building block would represent a specific value by mixing a UILabel (the text) and an image;
use a pan gesture recognizer, or alternatively define touchesBegin/Moved/Ended method to deal with panning;
when a pan is done, you displace the view content to the left or right according to the panning;
when panning, you also add new building blocks to the left or right end of the picker to account for empty areas that would be revealed by the displacement done at point 4.
I think that having a look at another kind of custom control source code would be of great help to you. You would not possibly find your custom picker already implemented, but could get some guidance. Have a look then at cocoa controls.
Hope this helps.
If I were going to implement this, I would create a really wide image that had every weight on it I'd ever need - I would probably create this in code when the app started up. This image is then used as the contentView of your picker. You get all the scrolling features "for free", and you could even update the values shown in the other parts of the view during scrolling (or dragging.
The scrollView is just the area with the tick marks and weight numbers, and resides in a subview above the background, but below the centered vertical line that shows the actual weight.
EDIT: on second thought, forget the image. If you have the code to draw the image, you can do the drawing in a custom UIView. So you get the draw rect, you know the contentOffset, so you can draw just what you need.

Looking to mimic the iPhone lock screen camera drag

I've been searching and searching on how apple makes the dragging motion on the lock screen to open the camera so clean. I'm trying to do a similar thing with an app where you drag from the bottom up to reveal a menu, but i cant quite get it right. Anyone know how to, or of any tutorials that show how to do this? Thanks in advance!
To follow up what's been mentioned by Hejazi I believe you can achieve this in 3 steps:
create a background rectangle with some corner radius (this is a property of CGRect).
create a top view, corresponding to the part you want to be able to slide. Attach a pan gesture to this view so you will be able to handle the animation for this view.
for the text part being highlighted I think you need another two views: I will apply a mask corresponding to the text to a view so you get some transparency only for the letters of your text and animate a white round view behind it.

UIGesture recognition on different areas of a UIImageView

I have this image:
What I want to do is to add a UITapGestureRecognizer to this image (or I can split the image in the different parts it consists of and add for each part a UITapGestureRecognizer) in order to have different actions according to the leaf tapped. If I split the image in different images each for each leaf the UIImageViews will probably overlap and tapping on one will be recognized as a tap on another one. Having just one image implies knowing the points of the screen that belongs to a leaf rather than to another one.
Any clues on how to do it would be really appreciated.
Thanks
Change your behavior by examining the gesture recognizer's locationInView:.
If you handle the image as one unit, implement this in your gesture recognizer call back to decide which "leaf" (if any) was tapped.
If you handle the image as multiple images, you could also implement it in your callback, or you could also implement in, e.g., your delegate's gestureRecognizerShouldBegin: to suppress events for touches outside the leaf as drawn.
EDIT: I didn't realize that you might also be looking for assistance on figuring out whether a point lies within a leaf. #PhillipMills is correct on this point: we need to know how you are drawing the image.
FOLLOW-UP: This is somewhat outside my area of expertise.
The easiest approach (from a hit-testing standpoint) is to do what #PhillipMills suggested, using Quartz drawing and CGPathContainsPoint(). If you have detailed graphics that you need rendered as a PNG, you could certainly construct a simple path that would be (virtually) overlayed to allow hit testing.
Your other options, AFAIK, are to do hit testing mathematically, but you would basically be reimplementing CGPathContainsPoint() but without a path, or to employ various tricks that look at the color of the pixels at your touch point to do hit testing. Googling will turn up some useful results if you go this route, but honestly for a shape as simple as what you've drawn, just use some UIBezierPath code to recreate in code.
Not sure if this will be helpful but if you get stuck on figuring out which leaf was clicked, you could use an old image map trick we used to use in CD-ROM projects for pixel accurate click tracking on images.
You have your full size image. Make a 25% (or less) scaled version of it. Fill each of the leaf regions you want to track clicks on with a different color; anything you want to ignore make black. When the full size image is clicked, get the x/y coordinates and scale them by the percentage of your scaled image. Then get the pixel color of the scaled image at the scaled x/y coordinate. By determining the pixel color you will know which leaf was clicked.
Sounds clunky but it works really well and is fast.
(all that said, I don't think alpha areas of images trigger the gesture recognizer - so breaking the image up would be less complicated/code intensive.)
If you can break the shape apart into the constituent elements, then you can put each into it's own layer and use the method discussed in this stackoverflow discussion to determine which was touched: Hit Testing with CALayer using the alpha properties of the CALayer contents

Hiding a part of an image in iPhone-Game

I am working on a small game, in which the user can drag rows and columns of images to solve a puzzle.
The puzzle does not use the whole screen, the "puzzle area" is in a rectangle area from 0,160 to 320,480.
Now, if the user drags a column of the images up, the top image in that column would be displayed "out of" that puzzle area. How could I only have that portion of the image shown, that remains within the puzzle area. Like not showing the whole 50x50 Pixel of the image, but only the lower 40x50 Pixel etc.?
You just need to set the frames on your views properly. If your puzzle views frame ends at the (0,160) point on top, any subviews you've added to it; such as the puzzle pieces will be hidden when they move out of the frame.
Hopefully I'm not oversimplifying the problem, but one of the easier ways I can think of working this scenario is by maintaining a proper view structure while moving the puzzle pieces.
i.e. as long as the view on the top half of the screen is higher up on the view hierarchy than the puzzle pieces (you could ensure this by properly maintaining the subviews), the puzzle pieces will be hidden under the top view if they move beneath it.
Is this what you need?
Cheers.
I am just tryin to give u some ideas.. Hope u can implement with that...
First use touchesBegan and touchesMoved methods to detect how much pixels the image has been dragged. Simultaneously u can also move the frame of the image to a new coordinate , hiding a portion of the image....tat is out of puzzle area.....
Hope this helps....
myImageView.frame=CGRectMake(0,0,50,50);
Let the above one be the actual frame. Now u have received the amt of pixels thru which the user has moved the image from touches method. Let it be some y1. To hide a part of the imageview reframe the imageview like this ,
myImageView.frame=CGRectMake(0,-y1,50,50);
negative y1 since u want to move out of the screen..
Hope this helps.

Drawing rubber band line during user drag

In my iPhone app, I would like the user to be able to "connect" two of my views by:
1) starting a drag in View A
2) as they drag towards View B, a straight line with one end in View A and the other end under at the current drag point, animates in a rubber-band fashion
3) when/if they release in View B, the line is then shown between the two views
I've seen examples of dragging and dropping views, and other examples of animations, but I haven't seen one that is a simple example of this kind of user-directed animation. Any pointers towards examples or the specific docs I should be looking at would be appreciated.
If this turns out to be trivial - my apologies. Although I've done quite a bit of development, I'm just getting started in the iPhone SDK and Core Graphics.
Turns out it is pretty easy - don't think of the line as belonging to either view, create a third view that is transparent and not opaque, place it over the top of the other two views. It could be full screen or you could calculate the size and position that just covers your views. Detect taps in this third view and use core animation to display a line from the point you started drawing to the point the line ends. When the line ends then you can detect whether the input was valid and place start/end points in the appropriate views. Functions that you will find especially useful in this process are UIView convertPoint:toView: and beginAnimations:context:.
It will probably keep things easier if you leave the line drawing as part of a dedicated view and add lines to it as they are accepted, rather than try to record the lines as part of the unrelated views that you are connecting with the lines - probably you want an array or similar containing CGFloats so that you can recreate the line view as necessary using drawRect:.