2012-03-30 18 views
7

我使用Grand Central Dispatch方法在队列中执行我的应用程序的一些执行。我决定在该队列上计算按钮的帧。我希望我的应用能够在旋转后重新绘制缩放并计算新帧。下面是我做了一些伪代码解释:如何命名一段代码并用不同的方法调用它?

CGFloat a=123, b=24; 
    dispatch_async(drawingQue, ^{ 
     //needed loops to get the total button count-how many ones will be drawn et.. 
     for(int x=0;x<someCount<x++){ 
      for(int y=0;y<anotherCount;y++){ 

     //needed frame&name ect assingments 

     button.frame= CGRectMake(x+y, x-y, a, b); 
     [button setTitle:@"abc"];}} 
     }; 

这里我想是的,我怎么给这个区块的名称,并在

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
} 

委托方法重新使用它呢?例如,如果旋转是风景,我想用a=234而不是123 ..请任何帮助。在此先感谢..

回答

6

声明块类型的实例变量,并使用Block_copy保持块:

@interface My { 
    void (^myBlock)(void); 
} 
@end 

myBlock = Block_copy(^{ 
    ...block code... 
}); 

// later call it 
myBlock(); 

// don't forget to release it in dealloc 

这是副本块重要存放其字面的范围之外之前(^{...}),因为原始块存储在堆栈中,并在范围退出时死亡。

+0

重要的是要注意,提到“自我”或在自己藏在伊娃里的块内的任何伊娃都会产生保留周期,从而导致泄漏。你可以通过'__block id blockSelf = self;'指针引用ivars或者通过安排释放block _before_'dealloc'来打破循环。 – 2012-03-30 18:43:37

1

只是做一个@property这是一个块,存储,以后再使用它:

typedef void (^MyBlock)(CGFloat, CGFloat); 
... 
@property(readwrite, copy) MyBlock buttonFramesBlock; 
... 
@synthesize buttonFramesBlock; 
... 
self.buttonFramesBlock = ^(CGFloat a, CGFloat b){ 
    //needed loops to get the total button count-how many ones will be drawn et.. 
    for(int x=0;x<someCount<x++){ 
     for(int y=0;y<anotherCount;y++){ 

    //needed frame&name ect assingments 

    button.frame= CGRectMake(x+y, x-y, a, b); 
    [button setTitle:@"abc"];}} 
}; 
... 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    dispatch_async(drawingQue, ^{ 
     self.buttonFramesBlock(234,someOtherInt); 
    }); 
} 
1

首先,永远不变的主线程之外UI。所以,你应该修改你的代码是这样的:

dispatch_async(drawingQue, ^{ 
    // ... 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     button.frame= CGRectMake(x+y, x-y, a, b); 
     [button setTitle:@"abc"]; 
    }); 
}); 

其次,永远不变的方法shouldAutorotateToInterfaceOrientation内部UI。所有你必须做的内部方法是返回视图是否应该旋转或不旋转。例如,在某些情况下,如果您有视图控制器层次结构,即使您在shouldAutorotateToInterfaceOrientation中返回YES,视图也可能不会旋转。

所以,你应该打电话给你的代码里面的方法:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

这可以通过多种方式来实现。最简单的(也是我推荐的)是使用标准的Objective-C方法:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { // landscape 
     [self rotateButtonWithA:234 b:24]; 
    } else { // portrait 
     [self rotateButtonWithA:123 b:24]; 
    } 

} 

- (void)rotateButtonWithA:(CGFloat)a b:(CGFloat)b 
{ 
    dispatch_async(drawingQue, ^{ 
     // ... 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      button.frame= CGRectMake(x+y, x-y, a, b); 
      [button setTitle:@"abc"]; 
     }); 
    }); 
} 

你不需要从多个地方调用块本身。但是如果你仍然想这样做,这里有很多答案告诉你如何做到这一点。

+0

你的答案看起来很合理,但我该如何处理这种情况?如果我静态决定帧,当它旋转它看起来很可怕 – ilhnctn 2012-03-30 08:30:58

+0

在此先感谢。真的很好的答案 - 解释。 – ilhnctn 2012-03-30 08:50:53

相关问题