ฉันมีวิธีการที่วาดปุ่มวงกลมลงบน UIImageView
ปุ่มนี้ดูดีและทำงานได้อย่างสมบูรณ์แบบ อย่างไรก็ตาม พื้นที่ที่คลิกได้นั้นไม่ได้เป็นเพียงวงกลมที่วาดไว้เท่านั้น แต่ยังเป็นสี่เหลี่ยมดั้งเดิมทั้งหมดด้วย
นี่คือวิธีการของปุ่มในคลาส DrawMenu
:
func drawButton(superImageView: UIImageView, delegate: ButtonDelegate, x_of_origin: CGFloat, y_of_origin: CGFloat, width_of_oval: CGFloat, height_of_oval: CGFloat, want_to_test_bounds:Bool) -> UIButton {
var button = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
button.addTarget(delegate, action: "buttonTap:", forControlEvents: UIControlEvents.TouchUpInside)
// THIS IS WHERE THE ORIGINAL RECT IS DEFINED, and it currently the clickable area
button.frame = CGRect(x: x_of_origin, y: y_of_origin, width: width_of_oval, height: height_of_oval)
button.clipsToBounds = true
// This is where the look of circle is defined, and IS WHERE THE CLICKABLE AREA SHOULD BE
button.layer.cornerRadius = height_of_oval/2.0
if (want_to_test_bounds == true) {
button.layer.borderColor = UIColor.blackColor().CGColor
button.layer.borderWidth = 1.0
}
return button
}
และนี่คือการโทรใน ViewController:
override func viewDidLoad() {
super.viewDidLoad()
var drawMenu = DrawMenu()
var newButton = drawMenu.drawButton(imageView, delegate: self, x_of_origin: 100, y_of_origin: 150, width_of_oval: 100, height_of_oval: 100, want_to_test_bounds: true)
imageView.userInteractionEnabled = true
imageView.addSubview(newButton)
}
และเช่นเคย เรายินดีเป็นอย่างยิ่งสำหรับความช่วยเหลือ ขอบคุณ
แก้ไข หลังจากถูกระบุว่า [ซ้ำกัน]
ฉันขอขอบคุณที่คำถาม iOS: พื้นที่ Hit ที่ไม่เป็นรูปสี่เหลี่ยมสำหรับปุ่ม กำลังถามคำถามเดียวกันโดยพื้นฐาน และฉันรู้สึกขอบคุณที่ถามคำถามนี้
อย่างไรก็ตาม หลังจากต่อสู้กับปัญหามาหลายชั่วโมง ฉันเชื่อว่าวิธีแก้ปัญหาเดียวของคำถามที่อ้างถึงคือ:
"คุณสามารถบรรลุสิ่งนี้ได้โดยการแบ่งคลาสย่อย UIButton และจัดเตรียมของคุณเอง:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
// return YES if point is inside the receiver’s bounds; otherwise, NO.
}
เอกสาร UIView ของ Apple ให้รายละเอียด เช่น การยืนยันว่าจุดนั้นอยู่ในระบบพิกัดของผู้รับแล้ว"
ยังไม่เพียงพอเนื่องจากเกี่ยวข้องกับความยากพิเศษของคำถามของฉัน
โดยเฉพาะ:
1. "ขอบเขตของผู้รับ" ในกรณีนี้คืออะไร? ไม่สามารถมีอะไรเกี่ยวข้องกับการแก้ไขเดิมได้เนื่องจากจะให้ผลลัพธ์เดียวกันแก่ฉัน ดังนั้นมันจึงน่าจะเป็น return button
2. แม้ว่าจะเป็นอย่างนั้น ฉันจะนำสิ่งนั้นไปปฏิบัติได้อย่างไร? pointInside:
ไม่สามารถยอมรับข้อโต้แย้งเพิ่มเติมได้
3. ฉันยังนำสิ่งนี้ไปใช้กับคลาสต่างๆ อีกด้วย ซึ่งทำให้โซลูชันของฉันแตกต่างไปจากที่กล่าวไว้ข้างต้น
ฉันเข้าใจถึงความสำคัญของการหลีกเลี่ยงคำถามที่ซ้ำกัน อย่างไรก็ตาม ในกรณีนี้ ฉันเชื่อว่าไม่ใช่เรื่องไร้เหตุผลที่จะขอวิธีแก้ปัญหาที่ดีกว่าแบบที่ iOS มอบให้: พื้นที่ที่ไม่เป็นสี่เหลี่ยมจัตุรัสสำหรับปุ่ม
ขอขอบคุณอีกครั้ง.
pointInside:withEvent:
สำหรับปุ่มกลม: stackoverflow.com/a/9336838/77567 - person rob mayoff   schedule 12.09.2015layoutSubviews
และเพิ่มvar _path = UIBezierPath(ovalInRect: self.bounds)
เข้าไป จากนั้นสร้าง funcPathButton
ซึ่งมี ` var button = UIButton(frame: rect) button.layer.cornerRadius = height_of_oval/2.0 button.clipsToBounds = true var _path = UIBezierPath(ovalInRect: rect) ` - person user2176152   schedule 13.09.2015override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool { return _path.containsPoint(point) }
- person user2176152   schedule 13.09.2015UIButton
ไม่ใช่การสร้างอินสแตนซ์ของคลาสย่อยของปุ่มของคุณ - person rob mayoff   schedule 13.09.2015