显示临时的子视图

时间:2011-04-11 19:41:02

标签: iphone objective-c cocoa-touch ios uiview

我想要实现的是在没有用户干预的情况下在几秒钟内显示视图。与按下iphone上的音量控制时出现的振铃音量视图效果相同:

ringer

我有一个带图像的滚动视图,在图像中录制,声音开始播放,另一个点击它暂停。我想实现上述效果只是为了通知动作(显示播放/暂停图像)。

我希望我已经完美地解释了这个问题。

非常感谢你的帮助。

此致 哈维

1 个答案:

答案 0 :(得分:4)

假设您有一些继承自UIViewController的类。您可以使用以下代码:

const int myViewTag = 10001;
const int myInterval = 1; // define the time you want your view to be visible

- (void)someAction {
    //this could be your `IBAction` implementation
    [self showMyView];
    [NSTimer scheduledTimerWithTimeInterval:myInterval
                                     target:self
                                   selector:@selector(hideMyView)
                                   userInfo:nil
                                    repeats:NO];
}


- (void) showMyView {
    //you can also use here a view that was declared as instance var
    UIView *myView = [[[UIView alloc] initWithFrame:CGRectMake(100, 100, 120, 120)] autorelease];

    myView.tag = myViewTag;
    [self.view addSubview:myView];
}

- (void) hideMyView {
    //this is a selector that called automatically after time interval finished
    [[self.view viewWithTag:myViewTag] removeFromSuperview];
}

你也可以在这里添加一些动画,但这是另一个问题:)