触摸ImageView到另一个ViewController

时间:2012-09-21 04:14:30

标签: objective-c ios uiviewcontroller uiimageview

我有2个ViewControllers,我创建了一个UIImageView来显示iPhone上的Splash Screen。我在TestAppDelegate.m写道:

====================

splashScreen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

splashScreen.image = [UIImage imageNamed:@"Default.png"];

[self.window addSubview:splashScreen];

 sleep(6);

[splashScreen removeFromSuperview];

====================

我的问题是,

如果我触摸此imageview,我将转到第二ViewController

其他在睡眠后,自动转到第一ViewController

那么,有可能这样做吗?

2 个答案:

答案 0 :(得分:2)

这样做:

在appDelegate.h文件中添加UIGestureRecognizerDelegate。

@interface AppDelegate : UIResponder <UIApplicationDelegate,UIGestureRecognizerDelegate>

现在

splashScreen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashScreen.userInteractionEnabled = YES;
splashScreen.image = [UIImage imageNamed:@"Default.png"];
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
tapRecognizer.delegate = self;
[splashScreen addGestureRecognizer:tapRecognizer];
[tapRecognizer release];

[self.window addSubview:splashScreen];

sleep(6);
[splashScreen removeFromSuperview];
//add ViewController1 here
ViewController1 *objViewController1 = [[ViewController1 alloc]initWithNibName:@"ViewController1" bundle:nil];
[self.window addSubview:objViewController1.view];

现在,当在启动画面上点击时,将调用处理程序

- (void)handleTap:(UITapGestureRecognizer*)recognizer
{
   // Do Your thing. 
   if (recognizer.state == UIGestureRecognizerStateEnded)
   {
     [splashScreen removeFromSuperview]; //edited here
     ViewController2 *objViewController2 = [[ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil];
    [self.window addSubview:objViewController2.view];
   }
}

答案 1 :(得分:0)

是。这是可能的。在AppDelegate中保留NSTimer

在定时器写入代码的选择器中,以推送到第一个视图控制器。

在imageview和touch事件写入代码上放置Touch Recognizer以推送到第二个视图控制器。