Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

I am wanting to automatically draw a line that connects to which ever UIbutton that is touched.

ic00042ic00042 SheffieldPosts: 14New Users *
I have placed several buttons on my main screen and added a randomisation button to randomise the positions of the buttons. No I am wanting it to automatically draw a line that connects to which ever UIbuttons that is touched.
Tagged:

Replies

  • dev666999dev666999 Posts: 3,554New Users @ @ @ @ @
    Google... How to draw a straight line in CoreGraphics.

    It's very straightforward.
  • ic00042ic00042 SheffieldPosts: 14New Users *
    But will that enable the connection from Buttons too?
  • dev666999dev666999 Posts: 3,554New Users @ @ @ @ @
    All you need are two CGPoints to create that line using Coregraphics. Use the Center Property of the buttons to do that.
  • ic00042ic00042 SheffieldPosts: 14New Users *
    So when the user touches both CGPoints, a line will be automatically drawn?
  • dev666999dev666999 Posts: 3,554New Users @ @ @ @ @
    You need to review CoreGraphics, and how to draw that line first. Once you see how it is done, it will become obvious as to how you can do it with two buttons using the Center property to create the two CGPoints.

    Once programmed/coded, it will draw the line automatically on a button press.
  • ic00042ic00042 SheffieldPosts: 14New Users *
    Thank you. So I've figured out how to draw a line in Core Graphics now. I'm totally new to xcode so therefore there are a lot of stuff I don't understand. I don't know how to proceed to the next step with making the action with the buttons when pressed.
  • ic00042ic00042 SheffieldPosts: 14New Users *
    edited December 2015
    My code for the line was

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, [UIColor
    yellowColor].CGColor);
    CGContextMoveToPoint(context, 30, 30);
    CGContextAddLineToPoint(context, 300, 400);
    CGContextStrokePath(context);
    }

  • dev666999dev666999 Posts: 3,554New Users @ @ @ @ @
    CoreGraphics drawing has to be done in a UIView. So, create a UIView, and connect it to your view controller. Inside your UIView you will add DrawRect which holds your drawing code. DrawRect is never called directly. You update each iteration of DrawRect using SetNeedsDisplay. Transfer your buttons to this UIView. All buttons have a center property... button.center. In your line code change the following lines to:

    CGContextMoveToPoint(context, button1.center.x, button1.center.y);
    CGContextAddLineToPoint(context, button2.center.x, button2.center.y);

    Use an IF statement or SWITCH to add additional buttons for button2 so that it can decide which button is pressed.


    To refresh it... ie...draw the line when button2 is pressed, inside your button2 method...

    -(void)mybuttondoesthis {

    Do something; //your original code

    [self setNeedsDisplay]; //updates the view for the drawn line.

    }

    That's it at it's simplest. If you want the lines to disappear, and reappear when another button is pressed, you can Google that.
  • ic00042ic00042 SheffieldPosts: 14New Users *
    edited December 2015
    Ok so I've gotten it to draw a line between my 2 buttons. But I am wanting the line to only appear when the user touches the 2 buttons instead?
  • dev666999dev666999 Posts: 3,554New Users @ @ @ @ @
    edited December 2015
    When the button is pressed the line is created. Now you need to erase that line when the other button is pressed, and a new line then gets drawn to that button. IE... clear your screen and redraw it.

    You can find examples of how to do that online. I am assuming that's what you're asking?

    If not, look at my last post, and see "setNeedsDisplay". That redraws the view. You can Google that, and see how it is used in methods... such as your button method.

    Tag each button, and use the Tag property with an IF stmt inside DrawRect to determine which button was pressed.
  • ic00042ic00042 SheffieldPosts: 14New Users *
    So the "setNeedsDisplay" should be all done under button 2 in the .h folder? Will it be the .h folder of my new UIView class or my ViewController.h?
  • dev666999dev666999 Posts: 3,554New Users @ @ @ @ @
    Your button, in the UIView, has a method. That method is in the .m file. Add...

    [self setNeedsDisplay];

    to your button's method.
  • ic00042ic00042 SheffieldPosts: 14New Users *
    My code in the UIView.m is only to draw the line. There is no code for my button in the .m file?
  • dev666999dev666999 Posts: 3,554New Users @ @ @ @ @
    OK... Let me ask you this.

    How much programming experience do you have in Objective C and using Xcode?

    The questions you are asking outside of drawing a line, seem extremely basic. Almost as if this is your first project. Most of what I am describing is basic stuff.
  • ic00042ic00042 SheffieldPosts: 14New Users *
    Yes sorry this is my first project, I've only started learning to use it a few weeks ago. Sorry about all the hassel!!
  • dev666999dev666999 Posts: 3,554New Users @ @ @ @ @
    Not a hassle. We're all here happy to help. However, you should probably do some reading on iPhone programming and get up to speed, so that you're familiar with the basics.
  • ic00042ic00042 SheffieldPosts: 14New Users *
    Ok, I will be doing that since I don't have much of an idea on programming. But would you be able to help guide me through the last steps you mentioned previously for the self selfNeedsDisplay part please?
  • ic00042ic00042 SheffieldPosts: 14New Users *
    edited December 2015
    I

    Post edited by ic00042 on
Sign In or Register to comment.