how to draw a picture in iPhone with Quartz2d - ios

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!

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;
}

ios locationInView not show correct log when I click direct view

I feel strange, I have dark view(darkScreenView) and green view.
I using the
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchViewPoint = [touch locationInView:touch.view];
NSLog(#"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkView pointInside:touchViewPoint withEvent:event]);
}
When I click on the dark view or green view,
The Log still show 1. why?
I click on the green view should show log 0, but I still get the log result 1.
I am using xcode 6.3.1.
Have anyone know what problem in this situation?
Thank you~
This will work
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchViewPoint = [touch locationInView:touch.view];
CGPoint pointOfDarkView = [touch.view convertPoint:touchViewPoint toView:self.darkview];
NSLog(#"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkview pointInside:pointOfDarkView withEvent:event]);
Your problem:
touchViewPoint is relative to touch.view coordinate
You have to convert point to darkview coordinate first,then you can use pointInside:pointOfDarkView
Try to get touch for different views..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if (touch.view==GREEN_VIEW)
{
//Write your code for green view here
}
else if(touch.view==DARK_VIEW)
{
//Write your code for dark view here
}
}
Your touchViewPoint's location is relative to the touched view. So get location relative to self.darkView like below.
CGPoint touchViewPoint = [touch locationInView: self.darkView];
Here is the problem change to View you are referring to.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchViewPoint = [touch locationInView:darkView]; <----- Problem
NSLog(#"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkView pointInside:touchViewPoint withEvent:event]);
}

how to make an object draggable?

hey guys i was wondering if there was away to make an object like a UIImageView draggable. I am aware of the UITouch and touches i.e.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *drag=[[event allTouches] anyObject];
player.center=[drag locationInView:self.view];
}
but in this way it is possible for the user to make the object jump across the screen to where he or she touches the screen but i want the user to have to manually drag the object across the screen.
please explain with code and let me know if i have to be more specific or clear...
You can do this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *aTouch = [touches anyObject];
CGPoint location = [aTouch locationInView:self.view];
if(CGRectContainsPoint(self.playerView.frame,location)) {
[UIView beginAnimations:#"Dragging A DraggableView" context:nil];
self.playerView.center = location;
[UIView commitAnimations];
}
}
This should give you a smooth animation.
if you want your user to drag&drop, first you have to check if the touch is inside the imageview rect frame. To improve the user experience, you can detect the touch offset from the imageview center; a good place to do this is inside the touchesBegan:withEvent:. After that, you have to change the position of the imageview, like this:
CGPoint touchOffset; //insert this inside your interface private iVars
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *drag=[[event allTouches] anyObject];
CGPoint touchLocation = [drag locationInView:self.view];
touchOffset = CGPointMake(player.center.x-touchLocation.x,player.center.y-touchLocation.y)
if(CGRectContainsPoint(player.frame,touchLocation)
player.center = CGPointMake(touchLocation.x+touchOffset.x,touchLocation.y+touchOffset.y);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *drag=[[event allTouches] anyObject];
CGPoint touchLocation = [drag locationInView:self.view];
if(CGRectContainsPoint(player.frame,touchLocation)
player.center = CGPointMake(touchLocation.x+touchOffset.x,touchLocation.y+touchOffset.y);
}
P.S. - Next time try to search similar questions...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* aTouch = [touches anyObject];
UIView* aView = [aTouch view];
if (aView != self.view) {
[self.view bringSubviewToFront:aView];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* aTouch = [touches anyObject];
UIView* aView = [aTouch view];
if (aView != self.view) {
aView.center = [aTouch locationInView:self.view];
}
}

objective c - Rotating image using CFAffineMakeRotation

I have been trying to get a UIImageView to rotate around a point when I touch and drag on it. I am having the problem where it jumps back to its original position if I try and rotate it a second time. I have tried the solution here, but that makes the image spin crazily!
What am I doing wrong? Here's my code:
Updated code with fixes:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (CGRectContainsPoint(Game10Wheel.frame, location)) {
Game10Angle = atan2([Game10Wheel center].y - location.y, [Game10Wheel center].x - location.x);
Game10WheelTouched = true;
}
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (Game10WheelTouched) {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
float theAngle = atan2([Game10Wheel center].y - location.y, [Game10Wheel center].x - location.x);
[Game10Wheel setTransform: CGAffineTransformRotate([Game10Wheel transform], theAngle - Game10Angle)];
Game10Angle = theAngle;
}
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
Game10WheelTouched = false;
}
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
Game10WheelTouched = false;
}
Thanks!
Your problem in calculation of the angle of rotation for further transformation. Try use code below.
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
float theAngle = atan2([Game10Wheel center].y - location.y, [Game10Wheel center].x - location.x);
[Game10Wheel setTransform: CGAffineTransformRotate([Game10Wheel transform], theAngle - Game10Angle)];
Game10Angle = theAngle;
}
You need to modify the current transformation of the object, otherwise you are just setting it to an initial value. Using CGAffineTransformMakeRotation creates a new transform object with only the rotation specified applied to it. What you want to do is use CGAffineTransformRotate and pass the current transform of the Game10Wheel object to add the rotation to that current transform.
EDIT: In other words, when you create your rotation, it should start from the current transform of the Game10Wheel, not from a new transformation.

Dragable UIImageView is Teleporting when tapping somewhere else

The code below is based on the idea of how to make a UIImageView be able to be dragged around the ViewController. When I use this code however, I click on a different location instead of pressing on the Image and it teleports to that location instead of requiring me to always drag the image. I want the code below to only work when pressing on that specific image. Please Help-
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
image.center = location;
[self ifCollided];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
It's teleporting simply because you don't have any check for whether the user has in fact touched the image, so any touch causes the image to jump to that location. What you need to do is check if the user has touched the image, then move it only if they have. Try the following code in the ViewController, assuming 'image' is the draggable view:
You'll need to create a variable/property on your ViewController to track if the view is being dragged. If you need just one image, you can use a BOOL (the code below assumes you've done this, called isDragging).
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:image];
if([image pointInside:location withEvent:event]) {
isDragging = YES;
}
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
if(isDragging)
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
image.center = location;
[self ifCollided];
}
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
isDragging = NO;
}
This code basically does a check in touchesBegan and sets the property to true if you touch inside the image, moves it in touchesMoved if your initial touch was on the image, and finally unsets the check in touchesEnded.

Resources