iphone - 使用 Time Profiler 分析的审查方法

标签 iphone ios objective-c xcode

我已经使用 Time Profiler Instrument 来识别我的应用程序中的显着滞后。现在我知道问题出在哪里,但我不知道该怎么办。我正在使用 ARC 并且有一个调用方法“Go”的 viewController,如下所示。我也包含了 Time Profiler 结果的快照。有谁知道我该如何解决这个问题?谢谢!

这是该方法的代码:

- (void)go {

CGFloat width = self.imageViewA.bounds.size.width;

[UIView animateWithDuration:18.0 delay:0.0 options:UIViewAnimationOptionCurveLinear 
animations:^ {
    self.imageViewA.frame = CGRectOffset(self.imageViewA.frame, -width, 0);
    self.imageViewB.frame = CGRectOffset(self.imageViewB.frame, -width, 0);
    } 
completion:^(BOOL finished) {
    if (self.keepGoing) {
        // now B is where A began, so swap them and reposition B
        UIImageView *temp = self.imageViewA;
        self.imageViewA  = self.imageViewB;
        self.imageViewB = temp;
        self.imageViewB.frame = CGRectOffset(self.view.bounds, 
        self.view.bounds.size.width, 0.0);
        // recursive call, but we don't want to wind up the stack
        [self performSelector:@selector(go) withObject:nil afterDelay:0.0];
        }
    }];
}

这是我的时间文件的结果:

enter image description here

作为引用,我还包含了我的 .h 文件。

@interface ViewController : GAITrackedViewController

@property (weak, nonatomic) IBOutlet UIBarButtonItem *lButton;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *rButton;

@property (nonatomic, assign) BOOL keepGoing;
@property (nonatomic, strong) UIImageView *imageViewA;
@property (nonatomic, strong) UIImageView *imageViewB;

- (IBAction)handleLPressed:(id)sender;
- (IBAction)handleRPressed:(id)sender;
@end

最佳答案

也许这可行:

- (void)go {

CGFloat width = self.imageViewA.bounds.size.width;

__weak typeof(self) self_ = self;

[UIView animateWithDuration:18.0 delay:0.0 options:UIViewAnimationOptionCurveLinear 
animations:^ {
    __strong typeof(self) self = self_;
    self.imageViewA.frame = CGRectOffset(self.imageViewA.frame, -width, 0);
    self.imageViewB.frame = CGRectOffset(self.imageViewB.frame, -width, 0);
    } 
completion:^(BOOL finished) {
    __strong typeof(self) self = self_;
    if (self.keepGoing) {
        // now B is where A began, so swap them and reposition B
        UIImageView *temp = self.imageViewA;
        self.imageViewA  = self.imageViewB;
        self.imageViewB = temp;
        self.imageViewB.frame = CGRectOffset(self.view.bounds, 
        self.view.bounds.size.width, 0.0);
        // recursive call, but we don't want to wind up the stack
        [self performSelector:@selector(go) withObject:nil afterDelay:0.0];
        }
    }];
}

这样做是保持对 self 的弱引用,而不是将其复制到堆栈中的 block 中。我不确定,但也许这可行。

关于iphone - 使用 Time Profiler 分析的审查方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15355453/

相关文章:

ios - 从 Xib 呈现模态视图 Controller

objective-c - 在 RubyMotion 中使用@selector

ios - 如何使用快速委托(delegate)与 cocoa

ios - iOS和Parse.com后端

iphone - 将音频从 iPhone 传输到浏览器。有想法吗?

ios - 多个 UIScrollViews 作为 UIScrollView 中的 subview Controller 不滚动到顶部

ios - Distriqt 本地通知在 iOS 上不显示 "body"属性

iphone - 更改 navigationItem.title 的位置

iphone - iOS 应用错误 : I screwed up somewhere in date calculation

iphone - 在 iOS/iPhone 中等待回调函数返回