Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Trying to Restrict the Hit Area of my Button to the Defined Circle across Classes.

Hi, I've been working on this problem for a number of weeks now, and the learning curve has grown so steep that I have gridded to halt. I'm having problems constructing the logic of implementing this between my class and the "ViewController"

In my "DrawButton" (which is a subclass of "UIButton") I have:

var _path : UIBezierPath!
var _button : DrawButton

init (_path: UIBezierPath!, _button: DrawButton) {
    self._path = _path
    self._button = _button
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func createCirclularPathButton(imageView_that_is_being_annimated_on: UIImageView, delegate: ButtonDelegate, x_of_origin: CGFloat, y_of_origin: CGFloat, width_of_oval: CGFloat, height_of_oval: CGFloat, want_line: Bool, line_width: CGFloat?=nil, line_color: UIColor?=nil) -> UIButton
{

    var rect = CGRect(origin: CGPoint(x: x_of_origin, y: y_of_origin), size: CGSize(width: width_of_oval, height: height_of_oval))

   _button.frame = rect
    _button.layer.cornerRadius = height_of_oval/2.0
    _button.clipsToBounds = true

     _path = UIBezierPath(ovalInRect: rect) 

    _button.addTarget(delegate, action: "Tap:", forControlEvents: UIControlEvents.TouchUpInside)

    if want_line == true {
        let line = CAShapeLayer()
        if let lineWidthForUse = line_width {
            line.lineWidth = line_width!
            if let lineColorForUse = line_color {
                line.fillColor = UIColor.clearColor().CGColor
                line.strokeColor = line_color!.CGColor

                line.path = _path.CGPath
                imageView_that_is_being_annimated_on.layer.addSublayer(line)
            }
        }
    }
    return _button
}

 override func layoutSubviews() {
    super.layoutSubviews()
    var _path = UIBezierPath(ovalInRect: self.bounds)
}

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
    return _path.containsPoint(point)
}

And in my "ViewController" I have:

override func viewDidLoad() {
    super.viewDidLoad()

    var drawButton = DrawButton(_button: newButton)
     // Obviously this is going to throw an error because I have not defined  newButton yet, but I'm having trouble constructing the logic between the two classes

    var newButton = drawButton.createCirclularPathButton(imageView, delegate: self, x_of_origin: 100, y_of_origin: 225, width_of_oval: 150, height_of_oval: 150, want_line: true, line_width: 1, line_color: UIColor.blueColor())

    imageView.userInteractionEnabled = true
    imageView.addSubview(newButton)

If anyone can help me properly construct this logic, I would really appreciate it. Thank You