發表文章

目前顯示的是有「mac」標籤的文章

在iphone中如何讓某個uiview順著某個圓心轉

首先定義,把角度轉換成弧度 #define RADIANS( degrees ) ( degrees * M_PI / 180 )  [NSTimer scheduledTimerWithTimeInterval:0.01f                                     target:self                                   selector:@selector(testeeee)                                   userInfo:nil                                    repeats:YES] ; -(void)  testeeee{  if (angle > 360) {         angle = 0;     }              angle++;     //    NSLog(@"%f",angle);     int r = 100;     //    testView.f...

Objective-C的數學運算函式

在寫程式的時候,常常需要用到一些數學運算,這邊整理一下常用的。 pow(x,y) : x 的 y 次方 sqrt(x) : x的平方根 floor(x) : 小於 x 的最大整數 ceil(x) : 大於 x 的最小整數 exp(x) : e 的 x 次方 log(x) : 以 e 為底 x 的對數值 log10(x) : 以10為底 x 的對數值 abs(x) : 整數 x 的絕對值 fabs(x) : 浮點數 x 的絕對值 srand(x), rand() : 產生亂數

使用權重控制隨機選取

圖片
水果  |  Apple  | Orange | Banana | Mango ------------------------------------------ 權重  |   70    |   10   |   10   |   10 從這個表中,我們分派了各個權重,我們希望Apple最常被抽中,所以將權重設定較高的值。 因此我們從這個表來實現作法。 fruits = [[NSArray alloc] initWithObjects:@"apple",@"orange",@"banana",@"mango", nil]; //定義水果 weights = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:70],[NSNumber numberWithInt:10],[NSNumber numberWithInt:10],[NSNumber numberWithInt:10], nil]; //定義權重  int currentfruit=0;     NSMutableArray *weighedfruits = [[NSMutableArray alloc] init];         while (currentfruit<[fruits count]){ //step through each fruit[] element        for (int i = 0; i < [[weights objectAtIndex:currentfruit] intValue]; i++) {                  [weighedfruits addObject: [fruits objectAtIndex:currentfruit]]; ...

iOS 產生隨機亂數1~100

-(IBAction)generateNumber:(id)sender { int number = (arc4random()%100)+1; //Generates Number from 1 to 100. NSString *string = [NSString stringWithFormat:@"%i", number]; label.text = string }

Core Data 學習筆記

圖片
做到現終於了解Coredata是怎樣的操作了,之前一直以為會跟ios上的操作不一樣,但是後來 看完了Apple官方文件,發現到Apple其實真得很不錯,從Mac到ios上的用法都大致相同,很 多地方其實可以用ios的一些原理,反推回去,不過還是有些許的部份是不同的。 在Mac上的Coredata,是標榜著" write less code ",這可是這Coredata當時的發展目標,在Mac 上確實真的看到了,他真的要寫的code很少,幾乎是不太要寫到什麼代碼。很快就可以完成 一個資料庫的加入、移除、修改,這三大基本動作,就連關聯式資料庫的一些維護動作,他 也全都在背後全做好了。 到目前的階段大致上需要的功能都有了,接下來可以專心的想如何開發出Mac App了。 以下是基本操作的代碼 在這個測試檔中,使用的Entity Class就叫做"Entity",使用的Model也是叫做Entity,裡面只有 一個欄位就叫做"name",標記為紅色的部份就是需要自已替換上的部份。 - (IBAction)fetchRequest:(id)sender {//取出所有資料          NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];     NSEntityDescription *entity = [NSEntityDescription entityForName:@" Entity " inManagedObjectContext:[self managedObjectContext]];     [fetchRequest setEntity:entity];          NSError *error = nil;     NSArray *fetchedObjects = [ [self managedObjectContext] executeFetchRequest:fetchRequest error:...