简体   繁体   English

将UITextField拖动到UIView周围(快速)

[英]Drag UITextField around UIView (Swift)

Here is my code 这是我的代码

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)
}

} }

I subclass my UITextField and i was trying to drag around the view but i don't know where i was doing wrong.When first touch it can't be drag but on second touch it jumps to another position and can be draggable.Can anyone help me solved this, it been a while now still stuck on this.I was trying to do something like snapchat textfield which is draggable. 我将我的UITextField子类化,并且试图在视图周围拖动,但是我不知道自己在哪里做错了。帮助我解决了这个问题,已经有一段时间了,我一直在尝试做。

    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()
    }
}

While nothing is said in the docs about dragging UITextField 尽管在文档中没有任何有关拖动UITextField的内容

This seem like a bad idea (my opinion) as the purpose of a UITextField is to accept user text input, however, I did test successfully dragging a UIView with a UITextField inside the UIView with a UIView parent. 这似乎是个坏主意(我认为),因为UITextField的目的是接受用户文本输入,但是,我确实测试成功将带有UITextField的UIView拖动到带有UIView父级的UIView中。

I would delete all of the touches code from your UITextField. 我将从您的UITextField中删除所有的触摸代码。 Subclass the UIView and add a touchesMoved method to be overriding; 子类化UIView并添加要覆盖的touchesMoved方法; inside get the location and use that on the UIView.center. 在内部获取位置并在UIView.center上使用它。

Worked great for me. 对我来说很棒。 Tested both on swift 2.2 and 3. 在Swift 2.2和3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM