顯示具有 視窗 標籤的文章。 顯示所有文章
顯示具有 視窗 標籤的文章。 顯示所有文章

2017年7月1日 星期六

iOS Swift Custom View Xib 客製化 視窗

建立一個上下左右跑進來的視窗,使用xib客製化元件

1.建立一個xib檔案 File -> New -> File ... 選擇View
2.MyView.xib


a.建立 MyView.xib 並拉一個Label到視窗
b.選擇View
c.設定屬性Size:Freeform 其他None

3.建立MyView.Swift
//
//  MyView.swift
//  JumpViewTest
//
//  Created by Terry Yang on 2017/6/30.
//  Copyright © 2017年 terryyamg. All rights reserved.
//

import UIKit

class MyView: UIView {
    
    @IBOutlet weak var labCustom: UILabel!
    
    var view:UIView!
    
    // Label Inspectable
    @IBInspectable
    var mytitleLabelText: String? {
        get {
            return labCustom.text
        }
        set(mytitleLabelText) {
            labCustom.text = mytitleLabelText
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    func setup() {
        view = loadViewFromNib()
        view.frame = bounds
        view.autoresizingMask = [
            UIViewAutoresizing.flexibleWidth,
            UIViewAutoresizing.flexibleHeight
        ]
        addSubview(view)
    }
    
    func loadViewFromNib() -> UIView {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: "MyView", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        
        return view
    }
    
}

4.在MyView.xib

a.選擇File;s Owner
b.點擊Identity並選擇Class 對應到 MyView

5.連結Label到MyView.swift
6.於Main.storyboard建立四個按鈕

7.ViewController.swift


//
//  ViewController.swift
//  JumpViewTest
//
//  Created by Terry Yang on 2017/6/30.
//  Copyright © 2017年 terryyamg. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var indexOfTop : Int    = 0
    var indexOfLeft : Int   = 0
    var indexOfRight : Int  = 0
    var indexOfBottom : Int = 0
    
    var topView : MyView?
    var leftView : MyView?
    var rightView : MyView?
    var bottomView : MyView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //  Top 按鈕
    @IBAction func btnShowTop(_ sender: UIButton) {
        indexOfTop += 1
        
        if topView == nil {
            //建立客製化View視窗
            topView = MyView(frame: CGRect(x: 0, y: -150, width: UIScreen.main.bounds.size.width, height: 150)) //設定初始位置與大小
            topView?.backgroundColor = .red //背景顏色
            topView?.labCustom.text = "Oh! Top View" //顯示文字
            self.view.addSubview(topView!) //加入此視窗
        }
        
        if indexOfTop % 2 == 1 { //點擊展開視窗
            UIView.animate(withDuration: 0.5, animations: {
                self.topView?.frame.origin.y += 150
            }, completion: nil)
        } else { //點擊收起視窗
            UIView.animate(withDuration: 0.5, animations: {
                self.topView?.frame.origin.y -= 150
            }, completion: nil)
        }
    }
    
    // Left 按鈕
    @IBAction func btnShowLeft(_ sender: UIButton) {
        
        indexOfLeft += 1
        
        if leftView == nil {
            leftView = MyView(frame: CGRect(x: -150, y: 0, width: 150, height: UIScreen.main.bounds.size.height))
            leftView?.backgroundColor = .brown
            leftView?.labCustom.text = "It's Left"
            self.view.addSubview(leftView!)
        }
        
        if indexOfLeft % 2 == 1 {
            UIView.animate(withDuration: 0.5, animations: {
                self.leftView?.frame.origin.x += 150
            }, completion: nil)
        } else {
            UIView.animate(withDuration: 0.5, animations: {
                self.leftView?.frame.origin.x -= 150
            }, completion: nil)
        }
        
    }
    
    // Right 按鈕
    @IBAction func btnShowRight(_ sender: UIButton) {
        
        indexOfRight += 1
        
        if rightView == nil {
            rightView = MyView(frame: CGRect(x: view.frame.maxX, y: 0, width: 150, height: UIScreen.main.bounds.size.height))
            rightView?.backgroundColor = .green
            rightView?.labCustom.text = "It's Right"
            self.view.addSubview(rightView!)
        }
        
        if indexOfRight % 2 == 1 {
            UIView.animate(withDuration: 0.5, animations: {
                self.rightView?.frame.origin.x -= 150
            }, completion: nil)
        } else {
            UIView.animate(withDuration: 0.5, animations: {
                self.rightView?.frame.origin.x += 150
            }, completion: nil)
        }
        
    }
    
    // Bottom 按鈕
    @IBAction func btnShowBootom(_ sender: UIButton) {
        indexOfBottom += 1
        
        if bottomView == nil {
            bottomView = MyView(frame: CGRect(x: 0, y: view.frame.maxY, width: UIScreen.main.bounds.size.width, height: 150))
            bottomView?.backgroundColor = .black
            bottomView?.labCustom.text = "Hi! Bottom View"
            self.view.addSubview(bottomView!)
        }
        
        if indexOfBottom % 2 == 1 {
            UIView.animate(withDuration: 0.5, animations: {
                self.bottomView?.frame.origin.y -= 150
            }, completion: nil)
        } else {
            UIView.animate(withDuration: 0.5, animations: {
                self.bottomView?.frame.origin.y += 150
            }, completion: nil)
        }
    }
    
    
}


 檔案下載:
https://github.com/terryyamg/JumpViewTest
 參考連結:
https://stackoverflow.com/documentation/ios/1362/custom-uiviews-from-xib-files#t=201706280220545656796
https://www.youtube.com/watch?v=EBYdsYwoJVI&t=492s
https://www.youtube.com/watch?v=MMgfnrddOxY

2015年6月8日 星期一

Android SlidingDrawer 滑動 視窗

上下左右的滑動視窗
1.res/layout/activity_main.xml


    

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
    

    

        

        
        
    


2.MainActivity.java
package tw.android;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class MainActivity extends Activity {

 private EditText etT, etB, etL, etR, etRotation;
 private Button btConfirm;
 private SlidingDrawer sd;
 private int left, top, right, bottom;

 /** Called when the activity is first created. */
 @SuppressLint("ShowToast")
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  etT = (EditText) findViewById(R.id.etT);
  etB = (EditText) findViewById(R.id.etB);
  etL = (EditText) findViewById(R.id.etL);
  etR = (EditText) findViewById(R.id.etR);
  etRotation = (EditText) findViewById(R.id.etRotation);
  btConfirm = (Button) findViewById(R.id.btConfirm);
  sd = (SlidingDrawer) findViewById(R.id.sd);

  /* 確認輸入 */
  btConfirm.setOnClickListener(new Button.OnClickListener() {

   public void onClick(View v) {
    /* 輸入SlidingDrawer距離上方位置 */

    left = Integer.parseInt(etL.getText().toString()); // 左
    top = Integer.parseInt(etT.getText().toString()); // 上
    right = Integer.parseInt(etR.getText().toString()); // 右
    bottom = Integer.parseInt(etB.getText().toString()); // 下

    if (left > 1000 || top > 1000 || right > 1000 || bottom > 1000) {
     Toast.makeText(getApplicationContext(), "不可大於1000",
       Toast.LENGTH_SHORT).show();

    } else {
     RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT,
       RelativeLayout.LayoutParams.WRAP_CONTENT);

     lp.setMargins(left, top, right, bottom);
     sd.setLayoutParams(lp);
     /* 輸入SlidingDrawer旋轉角度 */
     sd.setRotation(Integer.parseInt(etRotation.getText()
       .toString()));

     Toast.makeText(getApplicationContext(), "輸入完成",
       Toast.LENGTH_SHORT).show();
    }

   }
  });

  /* 打開監聽動作 */
  sd.setOnDrawerOpenListener(new OnDrawerOpenListener() {
   public void onDrawerOpened() {

    Toast.makeText(getApplicationContext(), "我被拉開了",
      Toast.LENGTH_SHORT).show();
    ;
   }
  });

  /* 關閉監聽動作 */
  sd.setOnDrawerCloseListener(new OnDrawerCloseListener() {

   public void onDrawerClosed() {
    Toast.makeText(getApplicationContext(), "我被關閉了",
      Toast.LENGTH_SHORT).show();
   }
  });

 }
}



參考來源:
http://examples.javacodegeeks.com/android/core/widget/slidingdrawer/android-slidingdrawer-example/
http://stackoverflow.com/questions/3695856/android-slidingdrawer-from-top