반응형
신속한 addsubview 및 제거
탭 한 번으로 하위보기를 추가하고 제거하고 싶습니다. 이것은 내 코드입니다.
/ * 하위보기를 추가하려면 * /
var testView: UIView = UIView(frame: CGRectMake(0, 0, 320, 568))
testView.backgroundColor = UIColor.blueColor()
testView.alpha = 0.5
testView.tag = 100
super.view.userInteractionEnabled = false
self.view.userInteractionEnabled = true
self.view.addSubview(testView)
/ * 하위보기를 제거하려면 * /
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as UITouch
let point = touch.locationInView(self.view)
if(testView.tag==100){
println("Tag 100")
testView.removeFromSuperview()
}
else{
println("tag not found")
}
}
그러나 제거가 작동하지 않습니다 누군가가 나를 도울 수 있습니까? 감사!
도움에 감사드립니다. 이것이 해결책입니다. 저는 서브 뷰를 생성했고 그것을 제거하기 위해 제스처를 추가했습니다.
@IBAction func infoView(sender: UIButton) {
var testView: UIView = UIView(frame: CGRectMake(0, 0, 320, 568))
testView.backgroundColor = UIColor.blueColor()
testView.alpha = 0.5
testView.tag = 100
testView.userInteractionEnabled = true
self.view.addSubview(testView)
let aSelector : Selector = "removeSubview"
let tapGesture = UITapGestureRecognizer(target:self, action: aSelector)
testView.addGestureRecognizer(tapGesture)
}
func removeSubview(){
println("Start remove sibview")
if let viewWithTag = self.view.viewWithTag(100) {
viewWithTag.removeFromSuperview()
}else{
println("No!")
}
}
최신 정보:
Swift 3+
@IBAction func infoView(sender: UIButton) {
let testView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
testView.backgroundColor = .blue
testView.alpha = 0.5
testView.tag = 100
testView.isUserInteractionEnabled = true
self.view.addSubview(testView)
let aSelector : Selector = #selector(GasMapViewController.removeSubview)
let tapGesture = UITapGestureRecognizer(target:self, action: aSelector)
testView.addGestureRecognizer(tapGesture)
}
func removeSubview(){
print("Start remove sibview")
if let viewWithTag = self.view.viewWithTag(100) {
viewWithTag.removeFromSuperview()
}else{
print("No!")
}
}
viewWithTag
주어진으로보기를 찾으려면 함수 를 사용해야합니다 tag
.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as UITouch
let point = touch.locationInView(self.view)
if let viewWithTag = self.view.viewWithTag(100) {
print("Tag 100")
viewWithTag.removeFromSuperview()
} else {
print("tag not found")
}
}
XCode 8 및 Swift 3을 사용하여이 코드를 테스트했습니다.
SuperView에 사용자 정의보기를 추가하려면 다음을 사용하십시오.
self.view.addSubview(myView)
Superview에서 사용자 정의보기를 제거하려면 다음을 사용하십시오.
self.view.willRemoveSubview(myView)
참고 URL : https://stackoverflow.com/questions/28197079/swift-addsubview-and-remove-it
반응형
'programing' 카테고리의 다른 글
Java의 상수에서 주석에 Enum 값을 제공하는 방법 (0) | 2020.11.17 |
---|---|
Powershell 스크립트에서 BAT 파일을 실행하는 가장 안전한 방법 (0) | 2020.11.17 |
마지막 반복마다 (0) | 2020.11.17 |
포커스가있는 HTML 페이지 요소를 확인하는 방법은 무엇입니까? (0) | 2020.11.17 |
PHP> = 5.3 엄격 모드에서 오류를 생성하지 않고 객체에 속성을 추가하는 방법 (0) | 2020.11.16 |