gpt4 book ai didi

ios - 在 UIView 周围拖动 UITextField (Swift)

转载 作者:行者123 更新时间:2023-11-30 13:25:43 27 4
gpt4 key购买 nike

这是我的代码

import UIKit

class ForwardTextField: UITextField {

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
self.nextResponder()!.touchesBegan(touches, withEvent: event)
NSLog("Textfield Touches Began")
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesMoved(touches, withEvent: event)
self.nextResponder()!.touchesMoved(touches, withEvent: event)
NSLog("Textfield Touches Moved")
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event)
self.nextResponder()!.touchesEnded(touches, withEvent: event)
NSLog("Textfield Touches Ended")
}

override func touchesCancelled(

touches: Set<UITouch>?, withEvent event: UIEvent?) {
super.touchesCancelled(touches, withEvent: event)
self.nextResponder()!.touchesCancelled(touches, withEvent: event)
//NSLog(@"Textfield Touches Cancelled");
}
// placeholder position

override func textRectForBounds(bounds: CGRect) -> CGRect {
return CGRectInset(bounds, 10, 10)
}
// text position

override func editingRectForBounds(bounds: CGRect) -> CGRect {
return CGRectInset(bounds, 10, 10)
}

}

我对我的UITextField进行了子类化,我试图在 View 中拖动,但我不知道我哪里做错了。第一次触摸时,它不能拖动,但第二次触摸时,它会跳转到另一个位置并且可以拖动。任何人都可以帮我解决这个问题,已经有一段时间了,我仍然停留在这个问题上。我试图做一些像 snapchat 文本字段这样的可拖动的事情。

    var textField = ForwardTextField()


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("TOUCHES BEGAN\n ")
print("self.textfield == nil = \(self.view.subviews.contains(self.textField))\n")




self.isDrawing = false
if !self.view.subviews.contains(self.textField){
self.textFieldY = 80.0
self.textField = ForwardTextField(frame: CGRectMake(0.0, self.textFieldY, self.view.frame.size.width, 40.0))
self.textField.borderStyle = .None
self.textField.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.4)
//self.textField.alpha = 0.4;
self.textField.textColor = UIColor.whiteColor()
self.textField.returnKeyType = .Done
self.textField.delegate = self
//ADDED
//self.textField.userInteractionEnabled = true
self.textField.multipleTouchEnabled = false
self.textField.exclusiveTouch = false
self.textField.textAlignment = .Center
self.view.addSubview(self.textField)
print("Show Text BOx")
if self.textField.hidden == true {
self.textField.hidden = false
}
self.textField.becomeFirstResponder()

}else {

let touch: UITouch = touches.first!
//NSLog(@"touchesBegan");
let point: CGPoint = touch.locationInView(self.view)
if self.view.subviews.contains(self.textField) && CGRectContainsPoint(self.textField.frame, point) && !self.textField.hidden{

self.isMovingText = true


}
if self.isDrawing {
self.lastPoint = point
}

}


}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("TOUCHES MOVED ")
print("ISMOVING TEXT = \(isMovingText)")
if self.isMovingText {
print("MOVESSSS")
let touch: UITouch = touches.first!
var point: CGPoint = touch.locationInView(self.view)
self.textFieldY = point.y
self.textField.frame = CGRectMake(0.0, point.y, self.textField.frame.size.width, self.textField.frame.size.height)
self.view.setNeedsDisplay()
}
if self.isDrawing {
//NSLog(@"touchmoved");
var touch: UITouch = touches.first!
var currentPoint: CGPoint = touch.locationInView(self.view)
self.image.drawAtPoint(CGPointMake(0.0, 0.0))
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y)
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y)
CGContextSetLineCap(UIGraphicsGetCurrentContext(), .Round)
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush)
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.drawColor.CGColor)
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),.Normal)
CGContextStrokePath(UIGraphicsGetCurrentContext())
self.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.lastPoint = currentPoint
self.view.setNeedsDisplay()
}
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

if self.isMovingText == true {
var touch: UITouch = touches.first!
var point: CGPoint = touch.locationInView(self.view)
self.textFieldY = point.y
self.textField.frame = CGRectMake(0.0, point.y, self.textField.frame.size.width, self.textField.frame.size.height)
self.view.setNeedsDisplay()
self.isMovingText = false
}
if self.isDrawing {
//NSLog(@"touchended");
self.image.drawAtPoint(CGPointMake(0.0, 0.0))
CGContextSetLineCap(UIGraphicsGetCurrentContext(),.Round)
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush)
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.drawColor.CGColor)
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y)
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y)
CGContextStrokePath(UIGraphicsGetCurrentContext())
CGContextFlush(UIGraphicsGetCurrentContext())
self.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.view.setNeedsDisplay()
}
}

最佳答案

虽然文档中没有提及拖动 UITextField

这似乎是一个坏主意(我认为),因为 UITextField 的目的是接受用户文本输入,但是,我确实测试成功地将带有 UITextField 的 UIView 拖动到带有 UIView 父级的 UIView 内。

我会从您的 UITextField 中删除所有触摸代码。子类化 UIView 并添加要重写的 TouchMoved 方法;在里面获取位置并在 UIView.center 上使用它。

对我来说效果很好。在 swift 2.2 和 3 上进行了测试。

关于ios - 在 UIView 周围拖动 UITextField (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37225062/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com