Drawing on Ipad [duplicate] - ios

Is there any function or algorithm to catch all the points when drawing in iPhone? Like in Android, where we have gethistoical points.

Check the methods touchesBegan:withEvent:, touchesMoved:withEvent:, touchesEnded:withEvent: and touchesCancelled:withEvent: to detect movements
For example using the method -touchesMoved:withEvent:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];
NSLog(#"%#", NSStringFromCGPoint(currentPosition));
}
For more info check the documentation of the UIResponder class

Related

Moving button like assistive touch

Hi I want to move a uibutton where even I want in my app like we have Assistive touch in iphone so as a same I want that kind of button in my app..Is it possible to do that If so please help out with code are some links for that
This question is asked so many times.... here is answer
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
button.frame.x = touchlocation.x;
button.frame.y = touchlocation.y;
}

Left and right touches to invoke different methods - Sprite Kit

I have been looking at how to invoke different touch methods depending on where the user touches on the screen. I presume this should be fairly simple in Sprite Kit but I can't find anything on it. Currently I am using the following to make the character jump.
How would I only use this method if it is touched on the left or the right of the screen? So a right touch calls one method and a left touch calls another. Thanks.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
if (isJumping == NO){
isJumping = YES;
SKSpriteNode* charNode = (SKSpriteNode*)[self childNodeWithName:#"character"];
[charNode runAction:jumpAnimation];
[charNode runAction:jumpMovement];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
if(touchLocation.x < self.size.width/2)
NSLog(#"left side");
if(touchLocation.x > self.size.width/2)
NSLog(#"right side");
}

Drag an image when touched on the object itself

I created a very simple app that contains a draggable UIImageView but I faced a problem that the image can be dragged without touching it. I'm sure that the hole issue is from anyObject but I have no idea how to replace it I tried self.view.image but it failed. So here's my code
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
lineView.center = location;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
any ideas?
Consider using a pan gesture recogniser. Add it only to the image view (and enable user interaction on the view). Now you will only get action method callbacks from the gesture when the view has actually been touched, your code becomes simpler, and you can move the view with that position.
Also, in your current code, it's weird to call touchesBegan: from touchesMoved:. If you keep that code (because you don't want to use a gesture), you should refactor the code that moves the view out into a different method and have both touchesBegan: and touchesMoved: call that method.
Try the following
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if ([lineView pointInside:location withEvent:event])
lineView.center = location;
}

Get historcial points ios

Is there any function or algorithm to catch all the points when drawing in iPhone? Like in Android, where we have gethistoical points.
Check the methods touchesBegan:withEvent:, touchesMoved:withEvent:, touchesEnded:withEvent: and touchesCancelled:withEvent: to detect movements
For example using the method -touchesMoved:withEvent:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];
NSLog(#"%#", NSStringFromCGPoint(currentPosition));
}
For more info check the documentation of the UIResponder class

how to draw a picture in iPhone with Quartz2d

I'm studying Quartz now and want to do a demo like this:
when your finger moves on the iPhone screen, it shows the track in red color。
The code is like:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
_firstPoint = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
_endPoint = [touch locationInView:self];
[self setNeedsDisplay];
_firstPoint = _endPoint;
}
then
- (void)drawRect:(CGRect)rect {
// Drawing code.
CGContextRef _context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(_context, 1, 0, 0, 1);
CGContextMoveToPoint(_context, _firstPoint.x, _firstPoint.y);
CGContextAddLineToPoint(_context, _endPoint.x, _endPoint.y);
CGContextStrokePath(_context);
}
Here,_firstPoint and _endPoint are CGPoint to record positions.
However, it doesn't show the track.
I don't know what is the problem.
Please give any tips.
Finally, I'd like to consultant whether it is right to fulfill such a kind of App.
thanks!
To your point about where the collection of points making up the lines is stored, it is not stored in this example.
EDITED
Yeah, to store them, I'd just add to a NSMutableArray.
something like
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!_points) _points = [[NSMutableArray array] retain];
UITouch *touch = [touches anyObject];
[_points addObject:[NSValue valueWithCGPoint:[touch locationInView:self]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
[_points addObject:[NSValue valueWithCGPoint:[touch locationInView:self]];
[self setNeedsDisplay];
}
The setNeedsDisplay is going to invoke the drawRect that's where you use the points and your draw methods.
you can read this tutorial to figured it out - may be it helps
http://www.ifans.com/forums/showthread.php?t=132024
i think you missed CGContextBeginPath(...) first of all
good luck!

Resources