12/01/2015

[Mac] 解決 您要讓應用程式 XXX 接受傳入的網路連接嗎 一直跳出

方法1: 先關掉該app 設定→防火牆→進階→允許名單找到該app移掉 再打開app 同意 方法2: 開啟終端機輸入 (可以先輸入codesign -v 後把app檔拉到終端機畫面會自動產生路徑)
codesign -v /Applications/xxx.app
如果會有/Applications/xxx.app: a sealed resource is missing or invalid 就是得把app檔刪掉重新安裝

10/20/2015

[php] 實作callback


用名稱呼
function run1($callback)
{
 $callback('123');
}

function funA($val)
{
 echo 'funA:'.$val;
}

run1('by run1');//funA:by run1

用物件傳(推薦)
class ClassB
{
 public function funB($val){
  echo 'ClassB:funB:'.$val;
 }
}

function run2(ClassB $callable){
 $callable->funB('by run2');
}

run2(new ClassB());//ClassB:funB:by run2

6/25/2015

[Android] setClickListener in custom view

直接設 setClickListener 沒反應
要加上

    private OnClickListener listener;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP) {
            if(listener != null) listener.onClick(this);
        }
        return super.dispatchTouchEvent(event);
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_UP && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER || event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
            if(listener != null) listener.onClick(this);
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void setOnClickListener(OnClickListener listener) {
        this.listener = listener;
    }

6/04/2015

解決 Error:Execution failed for task 'preDexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command finished with non-zero exit value 1

解決 Error:Execution failed for task 'preDexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command  finished with non-zero exit value 1

  • 檢查 用到的 jar libs 內是不是有重復到的元件

support v4 之類的最容易衝到



  • 檢查 做jar時的jdk是不是夠高

有些像<>  catch中直接 catch(InterruptedException | InterruptedException e)
要用到jdk 1.7



  • 檢查 做jar時的jdk是不是太高
jar用1.8
project 只用1.7時
考慮jar重包成1.7以下或project升級成1.8以上

5/15/2015

[Android] 下拉更新 SwipeRefreshLayout

Layout Xml

                <android.support.v4.widget.SwipeRefreshLayout
                    android:id="@+id/laySwipe"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                        <ListView
                            ... />
                </android.support.v4.widget.SwipeRefreshLayout>



Java Code

OnRefreshListener onSwipeToRefresh = new OnRefreshListener() {
        @Override
        public void onRefresh() {
            laySwipe.setRefreshing(true);
            //dosomething
            laySwipe.setRefreshing(false);
        }
    };

((SwipeRefreshLayout)findViewById(R.id.laySwipe)).setOnRefreshListener(onSwipeToRefresh);


幫listview補上下面的code避免滑動衝突


    private OnScrollListener onListScroll = new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            if (firstVisibleItem == 0) {
                laySwipe.setEnabled(true);
            }else{
                laySwipe.setEnabled(false);
            }
        }
    };

4/21/2015

[Android] 處理 Can't create handler inside thread that has not called Looper.prepare()


因為 UI 操作不能在其它 thread 做
直接用 activity 提供的 runOnUiThread 做掉

runOnUiThread(new Runnable(){ public void run(){ 
    //原本會報錯的ui操作
});

8/21/2014

[Unity] uGUI (4.6) EventSystem 事件系統 快速上手

4.6 新進的 EventSystem 事件系統 可以用在 UI 事件外也可以用在普通的 GameObject
終於不用自已打射線偵測點擊了

環境 4.6.0b17 因為才beta版所以變動的機率很大


  • 對 UI 偵聽事件:

這邊先用 Image 試

4/29/2014

[Unity] Assetbundle Shader 遺失的解決方案

物件打包成 AssetBundle 後有的物件上的材質的shader 會 lost 變一塊紫色

//assetbundle後shader會lost的處理
gameObject.renderer.material.shader =  Shader.Find(gameObject.renderer.material.shader.name); 

或是

    using UnityEngine;
    using System.Collections;
    public class ApplyShader : MonoBehaviour 
    {
        private Material[] thisMaterial;
        private string[] shaders;
        void Start () {
            thisMaterial = this.renderer.sharedMaterials;             
            shaders =  new string[thisMaterial.Length];
            for( int i = 0; i < thisMaterial.Length; i++){
                shaders[i] = thisMaterial[i].shader.name;
            }    

            for( int i = 0; i < thisMaterial.Length; i++){
                thisMaterial[i].shader = Shader.Find(shaders[i]);  
            }
        }
    }

3/21/2014

[MAC] 在 MAC 上安裝 nginx

安裝 Homebrew
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
安裝 nginx
brew install nginx
讓 nginx 在登入時自動載入
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
立刻載入
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

sudo nginx
網頁根目錄
/usr/local/var/www
nginx 的設定檔
/usr/local/etc/nginx/nginx.conf
關掉 apache
sudo apachectl stop

2/21/2014

2/20/2014

[iOS開發] UIImageView 加點擊範例

簡單的讓 UIImageView 變成 ImageButton imgBtn.png 放在 Resources 資料夾
//生成 UIImage 並指定圖片
UIImage *image = [UIImage imageNamed:@"imgBtn.png"];
UIImageView *iv = [[UIImageView alloc] initWithImage:image];
//指定位置 x,y,width,height
iv.frame = CGRectMake(10, 10, image.size.width, image.size.height);

//讓 UIImage 能接受使用者事件
iv.userInteractionEnabled = YES;

//UITapGestureRecognizer 是觸控控制類
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
[iv addGestureRecognizer:singleTap];

[self.view addSubview:iv];

-(void)tapDetected{
  NSLog(@"Click!!");
}

2/16/2014

[Android] 開啟另一個Activity


開啟另一個Activity ( A 呼 B
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivity(intent);

不同 Activity 之間傳值

2/14/2014

[Cocos2D] 在文字旁加上黑邊 CCLabelTTF and CCSprite


 static CCRenderTexture* createStroke(CCSprite* label, int size, ccColor3B color, GLubyte opacity)
 {
   
   CCRenderTexture* rt = CCRenderTexture::create(label->getTexture()->getContentSize().width + size * 2,
       label->getTexture()->getContentSize().height+size * 2);
   CCPoint originalPos = label->getPosition();
   
   ccColor3B originalColor = label->getColor();
   
   GLubyte originalOpacity = label->getOpacity();
   
   label->setColor(color);
   
   label->setOpacity(opacity);
   
   bool originalVisibility = label->isVisible();
   
   ccBlendFunc originalBlend = label->getBlendFunc();
   
   ccBlendFunc bf = {GL_SRC_ALPHA, GL_ONE};
   
   label->setBlendFunc(bf);
   
   CCPoint bottomLeft = ccp(
       label->getTexture()->getContentSize().width * label->getAnchorPoint().x + size,
       label->getTexture()->getContentSize().height * label->getAnchorPoint().y + size);
   
 //原來沒有判斷AnchorPoint的寫法
 //  CCPoint positionOffset= ccp(   - label->getTexture()->getContentSize().width / 2,
 //                                  - label->getTexture()->getContentSize().height / 2);
   
 //SetAnchorPoint會影響到positionOffset,所以要做判斷
   CCPoint positionOffset = CCPointZero;
   if(label->getAnchorPoint().x == 0.5f){
       positionOffset.x = 0;
   }else if(label->getAnchorPoint().x == 0.0f){
       positionOffset.x =- label->getTexture()->getContentSize().width / 2;
   }else{
       positionOffset.x = label->getTexture()->getContentSize().width/2;
   }
   if(label->getAnchorPoint().y == 0.5f){
       positionOffset.y = 0;
   }else if(label->getAnchorPoint().y == 0.0f){
       positionOffset.y =- label->getTexture()->getContentSize().height / 2;
   }else{
       positionOffset.y =  label->getTexture()->getContentSize().height/2;
   }
   
   CCPoint position = ccpSub(originalPos, positionOffset);
   
   rt->begin();
   
   for (int i=0; i<360 data-blogger-escaped-for="" data-blogger-escaped-i="" data-blogger-escaped-label-="" data-blogger-escaped-needs="" data-blogger-escaped-optimize="" data-blogger-escaped-should="" data-blogger-escaped-that="" data-blogger-escaped-you="" data-blogger-escaped-your="">setPosition(
       ccp(bottomLeft.x + sin(CC_DEGREES_TO_RADIANS(i))*size, bottomLeft.y + cos(CC_DEGREES_TO_RADIANS(i))*size)
       );
       label->visit();
   }
   rt->end();
   
   label->setPosition(originalPos);
   label->setColor(originalColor);
   label->setBlendFunc(originalBlend);
   label->setVisible(originalVisibility);
   label->setOpacity(originalOpacity);
   
   rt->setPosition(position);
 //反踞齒
   rt->getSprite()->getTexture()->setAntiAliasTexParameters();
   
   return rt;
}