« admin 发表的文章

[z] Create Your Own iOS Framework

If you’re new to programming in iOS and are coming from a .Net background (like myself), you may be wondering where all the dlls are.

Well, instead of dlls the platform works in terms Frameworks, which are basically folders thats contain public header files and a binary. Unfortunately, XCode doesn’t natively support the creation of your own Frameworks.

As a control developer, this is kind of a big deal, b/c without frameworks, we have to fall back on creating a static library file. The problem with lib files is they’re a pain to consume. Essentially you add the lib as a reference to your project, and then you have to tell the project where to look for the header files. And thats not a good User Experience.

So how do we get around this limitation?

After doing lots of searching, i stumbled across an awesome open source project on gitHub: iOS-Universal-Framework. The project is basically a project template. You simply install it and now have a new option when creating a project.

So lets take a look at the install process and how to use it:

1. Download the zip file.

2. Make sure you save the file somewhere for future use. B/c its a project template, anytime you update XCode, you’ll have to reinstall the project template. (Don’t worry the install process is really fast)

3. Make sure XCode is completely shut down.

4. Unzip the zip file and navigate to: Real Framework/install.sh

5. If you’re familiar with using Terminal, simply call bash on install.sh If you’re not familiar, you can simply rename the file to install.sh.command and just double click on it

6. Once it starts running it’s going to ask you: Where is XCode installed. If you haven’t touched the default install location of XCode (in Applications) just hit enter

7. Next it will ask to confirm where its going to install. Just type y and hit enter

8. It will ask you for your password, type it in and hit enter

9. After that you should see Process Complete.

10. Now open up XCode and open the New Project window

11. Under iOS select Framework & Library and you’ll see a new option called “Static iOS Framework”

12. Hit Next and name your framework and follow the normal steps for creating a new project.

So, now you have a new framework! But how do you use it?

You’ll basically treat it like any normal project. However, for any API’s you want made public you need to tell it.

Lets walk through an step by step example of a creating a Framework, exposing your public API’s and consuming it!

1. Create your framework using the new project dialog:

2. You’ll have an empty project now. So lets add a new class. In this case lets create a new UIView and call it MyView

3. Now, since I want these API’s to be available to consumers of this framework, i need to mark the “.h” file as being public. To do that click on the project in the project explorer and select build phases:

If you expand the Copy Headers section, you’ll see that there are 3 options, Public, Private and Project. By default, whenever you add a new file, it will show up under project. If you want to expose it as public, simply drag the header file from project and place it in public:

4. So now lets just a little bit of code to our class so that it does something.

@implementation MyView

-(id)init
{
self = [super init];
if(self)
{
self.backgroundColor = [UIColor orangeColor];
}
return self;
}

@end

We’ll simply make our View have a orange background color by default.

5. Now this next step is optional, but in my opinion is good practice. If you’ve ever noticed when you use Apple’s default frameworks, the import statement generally looks likes: #import UIKit/UIKit/.h  or #import  QuartzCore/QuartzCore.h  The nice thing about this, is if you know the name of the framework, then you know which header file to reference. And these header files expose ALL the public API’s in framework. So you don’t have to add a bunch of header files.

So lets do this now.

First open the Add New File dialog and add a new Header file:

Name the header file the exact same name you named your framework. And inside the header file, add a #import for every public header file in your framework.

Now, DON’T FORGET TO GO TO THE BUILD PHASES AND MARK IT AS PUBLIC!!!!!! (Step 3)

6. Now Build your project, and LETS USE IT!!!

7. Create a new project, for this case we’re just going to create a Single View Application called MyApp.

8. Once the project its created click on the project and scroll down to “Linked Frameworks and Libraries Section” and click the little plus button:

Now navigate to your framework by clicking the “Add Other” button:

Time to write some code!

9. First open up the ViewController.m file and write your code:

10. Run your project and you should see the following:

And there you go! You’ve written a framework which you can now share between multiple projects and teams!

As always i hope this was helpful.

For more tips ans trick, check out my blog.

By Stephen Zaharuk (SteveZ)

Spin like “Google Earth” in Unity

It’s complicated than I thought…

#pragma strict
 
var numberAverages : int = 3;
private var originalRotation : Quaternion;
private var offsetRotation : Quaternion;
 
// Make sure there are always SphereCollider and Rigidbody
@script RequireComponent (SphereCollider)
@script RequireComponent (Rigidbody)
 
function Awake () {
    numberAverages = Mathf.Clamp (numberAverages, 1, numberAverages);
}
 
function OnMouseDown () {
    var hit : RaycastHit;
    var dir : Vector3;
 
    // Stop spinning
    rigidbody.angularVelocity = Vector3.zero;
 
    // Record initial variables 
    if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
        originalRotation = transform.rotation;
        dir = hit.point - transform.position;
        offsetRotation = Quaternion.Inverse (Quaternion.LookRotation (dir));
        Spin (dir);
    }
}
 
function Spin (dir : Vector3) {
    var hit : RaycastHit;
    var privDirList : Array = new Array ();
    var currentDir : Vector3;
 
    // Initialize previous direction list
    for (var i : int = 0; i < numberAverages; i++) {
        privDirList.Add (currentDir);
    }
 
    currentDir = dir;
 
    // Make the object rotate with the cursor while we are grabbing it
    while (Input.GetButton ("Fire1") && Physics.Raycast (camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
        // Remove first element of the array
        privDirList.RemoveAt (0);
 
        // Add current direction to the end
        privDirList.Add (currentDir);
        currentDir = hit.point - transform.position;
        transform.rotation =  Quaternion.LookRotation (currentDir) * offsetRotation * originalRotation;
        yield;
    }
 
    // User let go of the mouse so make the object spin on its own
    var avgPrevDir : Vector3 = Vector3.zero;
    var newPDirList : Vector3[] = privDirList.ToBuiltin(Vector3);
    for (dir in newPDirList) {
        avgPrevDir += dir;
    }
 
    // Keep spinning with the direction when you end touching
    avgPrevDir /= numberAverages;
    MoveAcceleration (currentDir, avgPrevDir); 
}
 
function MoveAcceleration (r2 : Vector3, r1 : Vector3) {
    var linearVelocity : Vector3;
    var angVelocity : Vector3;
 
    // Calculate the angular velocity:  Omega = r x v / r^2
    linearVelocity = (r2 - r1) / Time.deltaTime;
    rigidbody.angularVelocity = Vector3.Cross (r2, linearVelocity) / r2.sqrMagnitude;
}

how to create instance in Unity

#1 Create Class Car.

using UnityEngine;
using System.Collections;
 
public class Car : ScriptableObject {
	public string color;
 
	void Awake(){
		color = "red";
	}
}

#2 Create instance.

using UnityEngine;
using System.Collections;
 
public class main : MonoBehaviour {
	void Start(){
		Car polo = (Car) ScriptableObject.CreateInstance(typeof(Car));
	}
}

Json Parser

A very useful tools for checking Json format error.
http://json.parser.online.fr/

[转]C# 中 List 用法

List 类是 ArrayList 类的泛型等效类,某些情况下,用它比用数组和 ArrayList 都方便。
我们假设有一组数据,其中每一项数据都是一个结构。

public struct Item
{
    public int Id;
    public string DisplayText;
}

注意结构是不能给实例字段赋值的,即 public int Id = 1 是错误的。

using System.Collections.Generic;
 
List<Item> items = new List<Item>();
 
//添加
Item item1 = new Item();
item1.Id = 0;
item1.DisplayText = "水星";
items.Add(item1);
 
//添加
Item item2 = new Item();
item2.Id = 1;
item2.DisplayText = "地球";
items.Add(item2);
 
//修改
//这里使用的是结构,故不能直接用 items[1].DisplayText = "金星";
//如果 Item 是类,则可以直接用。为什么呢?因为结构是按值传递的。
Item item = items[1];
item.DisplayText = "金星";
items[1] = item;

[转]C# []、List、Array、ArrayList 区别及应用

作者:vkvi 来源:千一网络(原创)

[] 是针对特定类型、固定长度的。
List 是针对特定类型、任意长度的。
Array 是针对任意类型、固定长度的。
ArrayList 是针对任意类型、任意长度的。
Array 和 ArrayList 是通过存储 object 实现任意类型的,所以使用时要转换。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // System.Int32 是结构
        int[] arr = new int[] { 1, 2, 3 };
        Response.Write(arr[0]); // 1
        Change(arr);
        Response.Write(arr[0]); // 2
 
        // List 的命名空间是 System.Collections.Generic
        List<int> list = new List<int>();
        list.Add(1);
        list.Add(2);
        list.Add(3);
        Response.Write(list[0]); // 1
        Change(list);
        Response.Write(list[0]); // 2
 
        // Array 的命名空间是 System
        Array array = Array.CreateInstance(System.Type.GetType("System.Int32"), 3); // Array 是抽象类,不能使用 new Array 创建。
        array.SetValue(1, 0);
        array.SetValue(2, 1);
        array.SetValue(3, 2);
        Response.Write(array.GetValue(0)); // 1
        Change(array);
        Response.Write(array.GetValue(0)); // 2
 
        // ArrayList 的命名空间是 System.Collections
        ArrayList arrayList = new ArrayList(3);
        arrayList.Add(1);
        arrayList.Add(2);
        arrayList.Add(3);
        Response.Write(arrayList[0]); // 1
        Change(arrayList);
        Response.Write(arrayList[0]); // 2
    }
 
    private void Change(int[] arr)
    {
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] *= 2;
        }
    }
 
    private void Change(List<int> list)
    {
        for (int i = 0; i < list.Count; i++) // 使用 Count
        {
            list[i] *= 2;
        }
    }
 
 
    private void Change(Array array)
    {
        for (int i = 0; i < array.Length; i++) // 使用 Length
        {
            array.SetValue((int)array.GetValue(i) * 2, i); // 需要类型转换
        }
    }
 
    private void Change(ArrayList arrayList)
    {
        for (int i = 0; i < arrayList.Count; i++) // 使用 Count
        {
            arrayList[i] = (int)arrayList[i] * 2; // 需要类型转换
        }
    }
}