2016-05-16 14 views
1

私はUIViewを持っています。私はUIImageViewです。また、この中にボタンがありますUIView。私がしたいことは、このボタンをクリックするとフリップアニメーションを作成し、私のUIImageViewを削除し、このスーパービューに別のビューをロードすることです。でも、私はこのフリップアニメーションが迅速に動作する仕組み2

func shareClick() 
{ 
    print("SHARE CLICK") 
    if showingBack { 

     UIView.transitionWithView(shareView, duration: 1.0, options: .TransitionFlipFromRight, animations: { 
      self.imgVwTop.removeFromSuperview() 
      }, completion: nil) 

     showingBack=false 

    } 

    else 
    { 
     UIView.transitionWithView(imgVwTop, duration: 1.0, options: .TransitionFlipFromRight, animations: { 
      self.shareView.removeFromSuperview() 
      }, completion: nil) 

     showingBack=true 

    } 


} 

のような何かをした私のボタンクリックで、私はebhaviourと困惑していると、それを行う方法を正確に理解していません。このボタンクリックイベントは、ここで何もしません。

答えて

4

:ここ

override func viewDidLoad() 
{ 
    super.viewDidLoad() 
    shareView = UIView(frame: CGRectMake(30, 100, 300, 400)) //set the frame of the holder view 
    flippedView = UIView(frame: shareView!.bounds) //setup flipped view 
    flippedView!.backgroundColor = UIColor.redColor() //for test 

    isFlipped = false //initially not flipped 

    //set up the initial view with image and button 
    aImageView = UIImageView(frame: shareView!.bounds) 
    aImageView!.image = UIImage(named: "1.jpg") 
    shareButton = UIButton(type: .System) 
    shareButton!.setTitle("share", forState: .Normal) 
    shareButton!.frame = CGRectMake(0, 0, 150, 50) 
    shareButton!.addTarget(self, action: "shareButtonAction", forControlEvents: .TouchUpInside) 

    //add both imageview and button to holder view 
    shareView!.addSubview(aImageView!) 
    shareView!.addSubview(shareButton!) 

    //finally add holder to self view 
    self.view.addSubview(shareView!) 
} 

を、あなたが削除することはできませんtransitionWithViewメソッドを使用する場合は、イメージビューのスーパービューを使用します。あなたができる最善の方法は、反転された後に表示したい新しいビューでイメージビューを置き換えることです。再度、サブビューとして追加することで、イメージビューに戻ることができます。例:

func shareButtonAction() 
{ 
    if (self.isFlipped! == false) 
    { 
     UIView.transitionWithView(shareView!, duration: 0.5, options:.TransitionFlipFromRight, animations: {() -> Void in 
      // self.aImageView!.image = UIImage(named: "2.jpg") 
     //hear remove the imageview add new view, say flipped view 
      self.aImageView!.removeFromSuperview() 
      self.shareView!.addSubview(self.flippedView!) 
     }, completion: { (Bool) -> Void in 
      self.isFlipped! = true 
      self.shareView!.bringSubviewToFront(self.shareButton!) //button should be top of the holder view 
     }) 
    } 
    else 
    { 
     UIView.transitionWithView(shareView!, duration: 0.5, options:.TransitionFlipFromRight, animations: {() -> Void in 
      //move back, remove flipped view and add the image view 
      self.flippedView!.removeFromSuperview() 
      self.shareView!.addSubview(self.aImageView!) 
      }, completion: { (Bool) -> Void in 
       self.isFlipped! = false 
       self.shareView!.bringSubviewToFront(self.shareButton!) 
     }) 
    } 
} 
1

コードのこの作品は、作品...

self.debug.hidden = true 
    let image = UIImage(data: data!) 
    self.debug.image = image 

    if (swipe.direction == UISwipeGestureRecognizerDirection.Left) { 
          UIView.transitionWithView(self.debug, duration: 1.0, options: [.TransitionFlipFromRight], animations: { 
           self.debug.hidden = false 
           }, completion: { _ in }) 
         } 

私はそれを隠した後、私は私のイメージに新しいイメージをロードします。

2

あなたは正しいアイデアを持っているように見えますが、私はあなたが何らかの種類のコンテナで反転させたいビューを正しく設定していると思います。

UIImageViewとボタンをプログラムで両方のインスタンスをインスタンス化すると役立つと思います。あなたがトランジションするとき、他のビューはそれらが即座に作成するために弱いようにリストされているので、アンロードされます。

私はアイデアをテストするためにビューコントローラを構成しました。最初はcurrentViewがストーリーボードからインスタンス化され、その後はプログラムによって作成されました。ボタンを押すたびに新しいビューが作成されます。アニメーションを実行して、次回ボタンが押されたときに新しいビューをcurrentViewとして設定します。

class ViewController: UIViewController { 

    @IBOutlet weak var currentView: UIView! 
    @IBOutlet weak var container: UIView! 
    var flipped = false 

    @IBAction func buttonPressed(sender: AnyObject) { 
     let new: UIView! 

     if flipped { 
      new = UIImageView(frame: container.bounds) 
      new.backgroundColor = UIColor.redColor() 
      flipped = false 
     } 
     else { 
      new = UIButton(frame: container.bounds) 
      new.backgroundColor = UIColor.blueColor() 
      flipped = true 
     } 


     let options: UIViewAnimationOptions = [.TransitionFlipFromLeft, .AllowUserInteraction, .BeginFromCurrentState] 
     UIView.transitionFromView(currentView, toView: new, duration: 0.5, options: options, completion: nil) 
     self.currentView = new 

    } 
} 

ホープこのことができます:)あなたは(私はshare viewはイメージ図とボタンが含まれていると)次の操作を行うことができます

関連する問題