Iphone 目标C:如何安排一系列连续的活动
Objective-C让我有点疯狂。我知道它不是一种一个接一个地处理每个命令的语言,但是如果我需要这样做呢 例如,我的问题是我想:Iphone 目标C:如何安排一系列连续的活动,iphone,objective-c,cocoa-touch,screenshot,Iphone,Objective C,Cocoa Touch,Screenshot,Objective-C让我有点疯狂。我知道它不是一种一个接一个地处理每个命令的语言,但是如果我需要这样做呢 例如,我的问题是我想: 拍摄我当前iPhone屏幕的截图 在将此屏幕截图添加到我的屏幕之前,我想添加另一个视图 然后添加屏幕截图,使另一个视图隐藏在下面 然后制作一个动画,将屏幕截图滑出视图,并显示隐藏在下面的内容 现在,应用程序将截取方法中添加的所有视图的屏幕截图(完全忽略我的事件顺序),并且不会将添加的视图隐藏在我的屏幕截图下面。无论我想做什么,一切都会立刻发生,这太糟糕了 这是我的代
- (void)takeScreenShot
{
screenShotView = [[UIImageView alloc] initWithImage:[self screenshot]];
[screenShotView setFrame:CGRectMake(0, -20, 320, 480)];
accessoryView.hidden = YES;
[self.view addSubview:accessoryView]; // which is hidden beneath and about to be revealed
[self.view addSubview:screenShotView];
[self.view bringSubviewToFront:screenShotView];
[self startAnimation];
}
- (void)startAnimation
{
[UIView animateWithDuration:0.0
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
accessoryView.hidden = NO;
}
completion:^(BOOL finished){
[UIView animateWithDuration:3.0
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
screenShotView.transform = CGAffineTransformMakeTranslation(-320, 0);
}
completion:^(BOOL finished){
}
];
}];
}
- (UIImage*)screenshot
{
// Create a graphics context with the target size
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
// Iterate over every window from back to front
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
{
// -renderInContext: renders in the coordinate space of the layer,
// so we must first apply the layer's geometry to the graphics context
CGContextSaveGState(context);
// Center the context around the window's anchor point
CGContextTranslateCTM(context, [window center].x, [window center].y);
// Apply the window's transform about the anchor point
CGContextConcatCTM(context, [window transform]);
// Offset by the portion of the bounds left of and above the anchor point
CGContextTranslateCTM(context,
-[window bounds].size.width * [[window layer] anchorPoint].x,
-[window bounds].size.height * [[window layer] anchorPoint].y);
// Render the layer hierarchy to the current context
[[window layer] renderInContext:context];
// Restore the context
CGContextRestoreGState(context);
}
}
// Retrieve the screenshot image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
这不是真正的答案,但评论没有提供足够的空间 我只是重新创建了一个简单的项目,看看添加视图的顺序是否对屏幕截图有任何影响 我使用了一个基于视图的应用程序模板。nib有两个按钮,分别连接到属性btn1和btn2。见截图1。上面的按钮btn1连接到一个动作,开始拍摄屏幕截图,并将其添加到按钮下方以查看差异。第二个按钮最初是隐藏的 屏幕截图1
- (void)viewWillAppear:(BOOL)animated
{
self.myView = [[[UIView alloc] initWithFrame:CGRectMake(0, 20, 320, 50)] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 40)];
myView.backgroundColor = [UIColor greenColor];
[myView addSubview:label];
label.text = @"fooo";
[label release];
}
- (IBAction)doTheThings
{
UIImageView *screenShotView = [[UIImageView alloc] initWithImage:[self screenshot]];
[screenShotView setFrame:CGRectMake(0, 230, 320, 480)];
btn2.hidden = NO;
[self.view addSubview:myView];
[self.view addSubview:screenShotView];
[screenShotView release];
}
这是我的viewController代码。myView是您的附件视图,它将在ViewWillDisplay上创建。此视图包含一个标签,稍后将看到
标题
...
@interface ScreenshotviewsViewController : UIViewController
{
UIButton *btn1;
UIButton *btn2;
UIView *myView;
}
@property (nonatomic ,retain) IBOutlet UIButton *btn1;
@property (nonatomic ,retain) IBOutlet UIButton *btn2;
@property (nonatomic ,retain) UIView *myView;
- (IBAction)doTheThings;
@end
我将跳过你的截图方法:没有改变,就像一个符咒一样有效:)。结果显示屏幕截图2
- 截图
- 显示btn2
- 将myView添加为子视图
- 将屏幕截图添加为子视图
- (void)viewWillAppear:(BOOL)animated
{
self.myView = [[[UIView alloc] initWithFrame:CGRectMake(0, 20, 320, 50)] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 40)];
myView.backgroundColor = [UIColor greenColor];
[myView addSubview:label];
label.text = @"fooo";
[label release];
}
- (IBAction)doTheThings
{
UIImageView *screenShotView = [[UIImageView alloc] initWithImage:[self screenshot]];
[screenShotView setFrame:CGRectMake(0, 230, 320, 480)];
btn2.hidden = NO;
[self.view addSubview:myView];
[self.view addSubview:screenShotView];
[screenShotView release];
}
屏幕截图2
案例二是
- 显示btn2
- 将myView添加为子视图
- 截图
- 将屏幕截图添加为子视图
- (IBAction)什么事 { btn2.hidden=否; [self.view addSubview:myView] UIImageView*screenShotView=[[UIImageView alloc]initWithImage:[自我截图]]; [screenShotView设置帧:CGRectMake(0、230、320、480)] [self.view addSubview:screenShotView]; [截屏视图发布]; }
如您所见,订单已被识别。不过我忽略了动画。删除动画,然后查看是否有效。或者像我一样在一个单独的项目中尝试一下,看看它是否能独立工作。如果是,我们将不得不深入了解您的应用程序。我可以问一下,您为什么要创建一个已经存在的视图的屏幕截图,并将其作为一个完整的图像显示吗?@Nick Weaver:谢谢您的提问!我想达到翻页的效果。所以,首先,我想拍一张“旧”或当前页面的照片,然后去掉旧页面并显示隐藏在下面的页面。。。这有意义吗?啊,我明白了。好的,accessoryView是否已经隐藏,这意味着在添加之前隐藏属性设置为“是”?请尝试在动画块中设置。您可以使用performSelector将显示部分移动到一个新方法,并稍微延迟调用它。好的,我将尝试一些简单的屏幕截图代码。需要几分钟。谢谢你的时间和努力。我会按照你的建议做,并在一个单独的项目中尝试。我做完后会告诉你的。再次非常感谢。我试过这个,效果很好。实际上你是对的,我的另一个应用程序有一个错误,我只是通过创建另一个项目才意识到有一个错误。非常感谢你的帮助。我会接受这是正确的答案,因为我认为这确实是我提出的问题的答案。非常感谢。我刚刚发布了一个后续问题,你可以在其中看到工作结果:()