Ios 绘制线条,触摸位置缩小并淡入背景Swift 3.0
我在Swift 3.0上遇到了一个奇怪的错误,我成功地在UIView中绘制了UIImageView,但是,当我绘制之前绘制的线时,它开始褪色并收缩到UIImageView的顶部,并且变得非常模糊,如所附图像所示。 如能提供任何帮助/指导,我将不胜感激Ios 绘制线条,触摸位置缩小并淡入背景Swift 3.0,ios,swift,uikit,Ios,Swift,Uikit,我在Swift 3.0上遇到了一个奇怪的错误,我成功地在UIView中绘制了UIImageView,但是,当我绘制之前绘制的线时,它开始褪色并收缩到UIImageView的顶部,并且变得非常模糊,如所附图像所示。 如能提供任何帮助/指导,我将不胜感激 class DrawView: UIView { var drawArea: UIImageView! var clearDrawArea: UIButton! var lastTouch = CGPoint.zero
class DrawView: UIView {
var drawArea: UIImageView!
var clearDrawArea: UIButton!
var lastTouch = CGPoint.zero
init(){
super.init(frame: CGRect(x: UIScreen.main.bounds.width*0.135, y:UIScreen.main.bounds.height*0.05, width: UIScreen.main.bounds.width*0.8, height: UIScreen.main.bounds.height*0.9))
drawArea = UIImageView(frame: CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.bounds.width*0.9, height: self.bounds.height*0.86))
drawArea.backgroundColor = UIColor.white
let buttonWidth = self.bounds.width*0.2
let buttonHeight = self.bounds.height*0.1
clearDrawArea = UIButton(frame: CGRect(x: (self.bounds.width*0.61)-(buttonWidth/2), y: self.bounds.height*0.94, width: buttonWidth, height: buttonHeight))
clearDrawArea.setTitle("Clear Draw Area", for: .normal)
clearDrawArea.backgroundColor = UIColor(red: 49/255, green: 200/255, blue: 134/255, alpha: 255)
clearDrawArea.addTarget(self, action: #selector(resetImage), for: .touchUpInside)
self.addSubview(clearDrawArea)
self.addSubview(drawArea)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let firstTouch = touches.first{
lastTouch = firstTouch.location(in: drawArea)
}
}
func drawLines(_ from: CGPoint, to: CGPoint){
UIGraphicsBeginImageContext(drawArea.frame.size)
let context = UIGraphicsGetCurrentContext()
context?.move(to: CGPoint(x: from.x, y: from.y))
context?.addLine(to: CGPoint(x: to.x, y: to.y))
context?.setLineCap(.round)
context?.setLineWidth(3)
context?.setStrokeColor(UIColor.black.cgColor)
context?.strokePath()
drawArea.image?.draw(in: drawArea.bounds)
drawArea.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let firstTouch = touches.first{
let touchLocation = firstTouch.location(in: drawArea)
drawLines(lastTouch, to: touchLocation)
lastTouch = touchLocation
}
}
func resetImage(){
drawArea.image = nil
}
}
类DrawView:UIView{
var绘图区域:UIImageView!
var clearDrawArea:UIButton!
var lastTouch=CGPoint.zero
init(){
super.init(帧:CGRect(x:UIScreen.main.bounds.width*0.135,y:UIScreen.main.bounds.height*0.05,width:UIScreen.main.bounds.width*0.8,height:UIScreen.main.bounds.height*0.9))
drawArea=UIImageView(帧:cRect(x:self.frame.origin.x,y:self.frame.origin.y,宽度:self.bounds.width*0.9,高度:self.bounds.height*0.86))
drawArea.backgroundColor=UIColor.white
let buttonWidth=self.bounds.width*0.2
let buttonHeight=自身边界高度*0.1
clearDrawArea=UIButton(边框:CGRect(x:(自边界.宽度*0.61)-(按钮宽度/2),y:自边界.高度*0.94,宽度:按钮宽度,高度:按钮宽度))
clearDrawArea.setTitle(“清除绘图区域”,用于:。正常)
clearDrawArea.backgroundColor=UIColor(红色:49/255,绿色:200/255,蓝色:134/255,alpha:255)
clearDrawArea.addTarget(self,action:#选择器(resetImage),for:.touchUpInside)
self.addSubview(clearDrawArea)
self.addSubview(绘图区域)
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
如果let firstTouch=touch.first{
lastTouch=firstTouch.位置(在:绘图区)
}
}
func绘图线(从:CGPoint到:CGPoint){
UIGraphicsBeginImageContext(drawArea.frame.size)
let context=UIGraphicsGetCurrentContext()
上下文?.move(到:CGPoint(x:from.x,y:from.y))
context?.addLine(to:CGPoint(x:to.x,y:to.y))
上下文?.setLineCap(.round)
上下文?.setLineWidth(3)
上下文?.setStrokeColor(UIColor.black.cgColor)
上下文?.strokePath()
drawArea.image?绘制(in:drawArea.bounds)
drawArea.image=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
如果let firstTouch=touch.first{
让touchLocation=firstTouch.location(在:绘图区域中)
抽绳(lastTouch,至:touchLocation)
lastTouch=触摸位置
}
}
func resetImage(){
drawArea.image=nil
}
}
稍微更改了代码,请尝试以下操作
class DrawView: UIView {
var drawArea: UIImageView!
var clearDrawArea: UIButton!
var lastTouch = CGPoint.zero
let pathAnimation: CABasicAnimation = CABasicAnimation(keyPath:"animation")
let tempPathLayer: CAShapeLayer = CAShapeLayer()
var lineLayer:CAShapeLayer=CAShapeLayer()
init(){
super.init(frame: CGRect(x: UIScreen.main.bounds.width*0.135, y:UIScreen.main.bounds.height*0.05, width: UIScreen.main.bounds.width*0.8, height: UIScreen.main.bounds.height*0.9))
drawArea = UIImageView(frame: CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.bounds.width*0.9, height: self.bounds.height*0.86))
drawArea.backgroundColor = UIColor.white
let buttonWidth = self.bounds.width*0.2
let buttonHeight = self.bounds.height*0.1
clearDrawArea = UIButton(frame: CGRect(x: (self.bounds.width*0.61)-(buttonWidth/2), y: self.bounds.height*0.94, width: buttonWidth, height: buttonHeight))
clearDrawArea.setTitle("Clear Draw Area", for: .normal)
clearDrawArea.backgroundColor = UIColor(red: 49/255, green: 200/255, blue: 134/255, alpha: 255)
clearDrawArea.addTarget(self, action: #selector(resetImage), for: .touchUpInside)
self.addSubview(clearDrawArea)
self.addSubview(drawArea)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let firstTouch = touches.first{
lastTouch = firstTouch.location(in: drawArea)
}
}
func drawLines(_ from: CGPoint, to: CGPoint){
UIGraphicsBeginImageContext(drawArea.frame.size)
//let context = UIGraphicsGetCurrentContext()
let tempPath: UIBezierPath = UIBezierPath()
var lineLayer:CAShapeLayer=CAShapeLayer()
tempPath.move(to: from)
tempPath.addLine(to: to)
let strokeColor = UIColor.red
UIColor.blue.setFill()
strokeColor.setStroke()
let tempPathLayer: CAShapeLayer = CAShapeLayer()
tempPathLayer.frame = self.bounds
tempPathLayer.position=self.layer.position
tempPathLayer.strokeColor = UIColor.green.cgColor
tempPathLayer.fillColor = UIColor.red.cgColor
tempPathLayer.lineWidth = 1.0
tempPathLayer.path = tempPath.cgPath
lineLayer=tempPathLayer
pathAnimation.beginTime=CACurrentMediaTime()
pathAnimation.duration = 2
pathAnimation.fromValue = NSNumber(value: 0.0)
pathAnimation.toValue = NSNumber(value:1.0)
lineLayer.add(pathAnimation, forKey: "animation")
self.layer.addSublayer(lineLayer)
drawArea.image?.draw(in: drawArea.bounds)
drawArea.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let firstTouch = touches.first{
let touchLocation = firstTouch.location(in: drawArea)
drawLines(lastTouch, to: touchLocation)
lastTouch = touchLocation
}
}
func resetImage(){
drawArea.image = nil
}
类DrawView:UIView{
var绘图区域:UIImageView!
var clearDrawArea:UIButton!
var lastTouch=CGPoint.zero
让路径动画:cabasicanition=cabasicanition(关键路径:“动画”)
让tempPathLayer:CAShapeLayer=CAShapeLayer()
变量lineLayer:CAShapeLayer=CAShapeLayer()
init(){
super.init(帧:CGRect(x:UIScreen.main.bounds.width*0.135,y:UIScreen.main.bounds.height*0.05,width:UIScreen.main.bounds.width*0.8,height:UIScreen.main.bounds.height*0.9))
drawArea=UIImageView(帧:cRect(x:self.frame.origin.x,y:self.frame.origin.y,宽度:self.bounds.width*0.9,高度:self.bounds.height*0.86))
drawArea.backgroundColor=UIColor.white
let buttonWidth=self.bounds.width*0.2
let buttonHeight=自身边界高度*0.1
clearDrawArea=UIButton(边框:CGRect(x:(自边界.宽度*0.61)-(按钮宽度/2),y:自边界.高度*0.94,宽度:按钮宽度,高度:按钮宽度))
clearDrawArea.setTitle(“清除绘图区域”,用于:。正常)
clearDrawArea.backgroundColor=UIColor(红色:49/255,绿色:200/255,蓝色:134/255,alpha:255)
clearDrawArea.addTarget(self,action:#选择器(resetImage),for:.touchUpInside)
self.addSubview(clearDrawArea)
self.addSubview(绘图区域)
}
必需的初始化?(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
//fatalError(“初始化(编码者:)尚未实现”)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
如果let firstTouch=touch.first{
lastTouch=firstTouch.位置(在:绘图区)
}
}
func绘图线(从:CGPoint到:CGPoint){
UIGraphicsBeginImageContext(drawArea.frame.size)
//let context=UIGraphicsGetCurrentContext()
让tempPath:UIBezierPath=UIBezierPath()
变量lineLayer:CAShapeLayer=CAShapeLayer()
tempPath.move(到:从)
tempPath.addLine(to:to)
让strokeColor=UIColor.red
UIColor.blue.setFill()
strokeColor.setStroke()
让tempPathLayer:CAShapeLayer=CAShapeLayer()
tempPathLayer.frame=self.bounds
tempPathLayer.position=self.layer.position
tempPathLayer.strokeColor=UIColor.green.cgColor
tempPathLayer.fillColor=UIColor.red.cgColor
tempPathLayer.lineWidth=1.0
tempPathLayer.path=tempPath.cgPath
lineLayer=tempPathLayer
pathAnimation.beginTime=CACurrentMediaTime()
pathAnimation.duration=2
pathAnimation.fromValue=NSNumber(值:0.0)
pathAnimation.toValue=NSNumber(值:1.0)
lineLayer.add(路径动画,forKey:“动画”)
self.layer.addSublayer(lineLayer)
drawArea.image?绘制(in:drawArea.bounds)
drawArea.image=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
如果let firstTouch=touch.first{
让touchLocation=firstTouch.location(在:绘图区域中)
抽绳(lastTouch,至:touchLocation)
lastTouch=触摸位置
}
}
func resetImage(){
drawArea.image=nil
}
}我终于让它正常工作了。我利用了你的想法,改用贝塞尔路径
class DrawView: UIView {
var drawArea: UIImageView!
var clearDrawArea: UIButton!
var lastTouch = CGPoint.zero
var context: CGContext!
var path: UIBezierPath!
init(){
context = UIGraphicsGetCurrentContext()
path = UIBezierPath()
super.init(frame: CGRect(x: UIScreen.main.bounds.width*0.135, y:UIScreen.main.bounds.height*0.05, width: UIScreen.main.bounds.width*0.8, height: UIScreen.main.bounds.height*0.9))
drawArea = UIImageView(frame: CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.bounds.width*0.9, height: self.bounds.height*0.86))
drawArea.backgroundColor = UIColor.white
let buttonWidth = self.bounds.width*0.2
let buttonHeight = self.bounds.height*0.1
clearDrawArea = UIButton(frame: CGRect(x: (self.bounds.width*0.61)-(buttonWidth/2), y: self.bounds.height*0.94, width: buttonWidth, height: buttonHeight))
clearDrawArea.setTitle("Clear Draw Area", for: .normal)
clearDrawArea.backgroundColor = UIColor(red: 49/255, green: 200/255, blue: 134/255, alpha: 255)
clearDrawArea.addTarget(self, action: #selector(resetImage), for: .touchUpInside)
self.addSubview(clearDrawArea)
self.addSubview(drawArea)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let firstTouch = touches.first{
lastTouch = firstTouch.location(in: drawArea)
}
}
func drawLines(_ from: CGPoint, to: CGPoint){
UIGraphicsBeginImageContext(drawArea.frame.size)
path.move(to: CGPoint(x: from.x, y: from.y))
path.addLine(to: CGPoint(x: to.x, y: to.y))
UIColor.black.setStroke()
path.lineWidth = 3
path.stroke()
context?.saveGState()
context?.addPath(path.cgPath)
context?.closePath()
context?.restoreGState()
drawArea.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let firstTouch = touches.first {
let touchLocation = firstTouch.location(in: drawArea)
drawLines(lastTouch, to: touchLocation)
lastTouch = touchLocation
}
}
func resetImage(){
path.removeAllPoints()
drawArea.image = nil
}