2016-06-30 5 views
1

だから、私はある種のパターンのカラスを持っています。私はこの円を線に変換したい。どのように私はこれを達成することができる任意のアイデア?円を素早く線に変換する方法は?

私は、UIBezierPathをサークル上に作成して、これを助けなしにラインに変換する方法を見てきました。私もopencv(fast Cartesian to Polar to Cartesian in Python)でこれを行うリンクを見たことがあるが、ネイティブの迅速にこれを行うことを好む。

enter image description here

+0

何か試しましたか?任意のコード? – Idan

+0

Iveは、UIBezierPathを作成して円をカバーし、これを線に変換してみましたが、運がありません。 –

+0

質問にベストを加えてみてください。誰かが助けてくれるかもしれません – Idan

答えて

1

トランジションの性質を気にしない場合は、最も簡単な方法は、別のパスからCAShapeLayerpathをアニメーション、CABasicAnimationを行うことです。そして、CAShapeLayerでパターン化されたパスを実現するには、実際には2つのオーバーラップするCAShapeLayerオブジェクトがあり、1つはダッシュパターンなしのオブジェクトです。

let fromPath = UIBezierPath(arcCenter: view.center, radius: 100, startAngle: 0, endAngle: CGFloat(M_PI) * 2.0, clockwise: true) 
let toPath = UIBezierPath() 
toPath.moveToPoint(CGPoint(x: view.frame.size.width/2.0 - CGFloat(M_PI) * 100.0, y:view.center.y)) 
toPath.addLineToPoint(CGPoint(x: view.frame.size.width/2.0 + CGFloat(M_PI) * 100.0, y: view.center.y)) 

let shapeLayer = CAShapeLayer() 
shapeLayer.path = fromPath.CGPath 
shapeLayer.lineWidth = 5 
shapeLayer.strokeColor = UIColor.redColor().CGColor 
shapeLayer.fillColor = UIColor.clearColor().CGColor 

let shapeLayer2 = CAShapeLayer() 
shapeLayer2.path = fromPath.CGPath 
shapeLayer2.lineWidth = 5 
shapeLayer2.strokeColor = UIColor.blackColor().CGColor 
shapeLayer2.fillColor = UIColor.clearColor().CGColor 
shapeLayer2.lineDashPattern = [100,50] 

view.layer.addSublayer(shapeLayer) 
view.layer.addSublayer(shapeLayer2) 

そして

降伏
shapeLayer.path = toPath.CGPath 
shapeLayer2.path = toPath.CGPath 

CATransaction.begin() 
CATransaction.setAnimationDuration(5) 

let animation = CABasicAnimation(keyPath: "path") 
animation.fromValue = fromPath.CGPath 
animation.toValue = toPath.CGPath 
shapeLayer.addAnimation(animation, forKey: nil) 

let animation2 = CABasicAnimation(keyPath: "path") 
animation2.fromValue = fromPath.CGPath 
animation2.toValue = toPath.CGPath 
shapeLayer2.addAnimation(animation, forKey: nil) 

CATransaction.commit() 

enter image description here

を使用すると、トランジションの性質をより細かく制御したい場合は、あなたがより多くの手動技術に頼る必要があり、例えばパスを手動で調整するCADisplayLinkがあります。しかしそれはより複雑です。

+0

こんにちは!そのような有益で良い答えをありがとう!唯一の問題は私の問題の策定でした。このサークルにはあらかじめ決められたパターンはありませんが、将来のアドバイスはImageですか? –

+0

ランダムなビットマップイメージを形状に変換してからその変換に進むことは自明ではありません – Rob

+0

私は誤解しているかもしれませんが、それは?私はパターンイメージで色を作ることができました: var color = UIColor(patternImage:image!)そしてstrokeColorを設定します。しかし、それは期待どおりに動作しませんでした。 画像をCAShapeLayerに追加することはできません.CALayerにはパスプロパティはありません。 私が見つけたのは: http:// stackoverflow。com/questions/5578801 /バックグラウンド画像を追加することは可能ですか? –

0

このコードを試してください。アニメーションは完璧ではありませんが、この方法で画像をラインに変換できます。私は上記のロブのソリューションがはるかに優れていると言わなければならないし、もし私が選択肢を与えたら、私は彼のやり方をするだろう。

以下のコードは非常に自明ですが、疑問があればお気軽にお問い合わせください。

アニメーションを行うには、黄色のボタンをタップします。

class ViewController: UIViewController{ 

    var imageView : UIImageView = { 
     let image = UIImageView() 
     image.translatesAutoresizingMaskIntoConstraints = false 
     image.layer.cornerRadius = 120 
     image.backgroundColor = UIColor.clearColor() 
     image.layer.borderWidth = 3.0 
     image.layer.borderColor = UIColor.blackColor().CGColor 
     return image 
    }() 

    var animatorButton : UIButton = { 
     let button = UIButton() 
     button.translatesAutoresizingMaskIntoConstraints = false 
     button.tintColor = UIColor.blueColor() 
     button.layer.borderWidth = 3.0 
     button.layer.borderColor = UIColor.blackColor().CGColor 
     button.backgroundColor = UIColor.yellowColor() 
     return button 
    }() 

    var constraintHeight = [NSLayoutConstraint]() 
    override func viewDidLoad() { 

     view.addSubview(imageView) 
     view.addSubview(animatorButton) 

     view.addConstraint(view.centerXAnchor.constraintEqualToAnchor(imageView.centerXAnchor)) 
     view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[imageView(240)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["imageView": imageView])) 

     self.constraintHeight = NSLayoutConstraint.constraintsWithVisualFormat("V:|-100-[imageView(240)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["imageView": imageView]) 
     view.addConstraints(constraintHeight) 


     view.addConstraint(animatorButton.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor)) 
     view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-450-[Button(40)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["Button": animatorButton])) 
     view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[Button]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["Button": animatorButton])) 


     animatorButton.addTarget(self, action: #selector(ViewController.animateImageToLine), forControlEvents: .TouchUpInside) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func animateImageToLine() { 

     view.removeConstraints(constraintHeight) 
     constraintHeight = NSLayoutConstraint.constraintsWithVisualFormat("V:|-100-[imageView(3)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["imageView": imageView]) 
     view.addConstraints(constraintHeight) 
     UIView.animateWithDuration(0.3, animations: { 
      self.imageView.layer.cornerRadius = 0 
       self.view.layoutIfNeeded() 


      }, completion: { success in 
       self.imageView.backgroundColor = UIColor.blackColor() 
       self.imageView.layer.borderWidth = 3.0 
     }) 

    } 
    } 
関連する問題