Could someone please tell me how to make an image appear when the user taps the screen and make it appear at the position of the tap. Thanks in advance, Tate
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- how do you prevent page scroll in textarea on mobi
- Custom UITableview cell accessibility not working
相关文章
- Could I create “Call” button in HTML 5 IPhone appl
- Unable to process app at this time due to a genera
- How do you detect key up / key down events from a
- “Storyboard.storyboard” could not be opened
- Open iOS 11 Files app via URL Scheme or some other
- Can keyboard of type UIKeyboardTypeNamePhonePad be
- Can not export audiofiles via “open in:” from Voic
- XCode 4.5 giving me “SenTestingKit/SenTestKit.h” f
You could create an initial star and just move it every time view is touched. I'm not sure what you're end result will look like.
Note: This code will give you 1 star that moves with a tap Here is my code:-
It seems implied from the question that you want the user to be able to tap anywhere on the screen and have an image drawn where they tap? As opposed to tapping in a designated place and having the image appear there?
If so, you're probably going to have to go with a custom view. In that case, you'd do something like the following:
UIView
.touchesBegan
method. Call[[touches anyObject] locationInView:self]
(wheretouches
is the first argument to the method, anNSSet
ofUITouch
objects) to get the location of the touch, and record it.touchesEnded
method. Determine the location touches ended at using the same method as in step 2.[self setNeedsDisplay]
to cause the custom view to be redrawn.drawRect
method. Here, if the location has been set in step 4, you can use theUIImage
methoddrawAtPoint
to draw your image at the selected location.For further details, this link might be worth a look. Hope that helps!
EDIT: I've notice you've asked essentially the same question before. If you're not happy with the answers given there, it's generally considered better to "bump" the old one, perhaps by editing it to ask for further clarification, rather than create a new question.
EDIT: As requested, some very brief sample code follows. This is probably not the best code around, and I haven't tested it, so it may be a little iffy. Just for clarification, the
THRESHOLD
allows the user to move their finger a little while tapping (up to 3px), because it's very difficult to tap without moving your finger a little.MyView.h
MyView.m
UIView
is a subclass ofUIResponder
, which has the following methods that might help:-touchesBegan:withEvent:
,-touchesEnded:withEvent:
,-touchesCancelled:withEvent:
and-touchesMoved:withEvent:
.The first parameter of each of those is an
NSSet
ofUITouch
objects.UITouch
has a-locationInView:
instance method which should yield the position of the tap in your view.