gpt4 book ai didi

ios - 在 ViewController 中显示动画 View

转载 作者:行者123 更新时间:2023-11-29 12:57:29 25 4
gpt4 key购买 nike

编辑:

我为我的 View 创建了一个幻灯片菜单,它按我想要的方式工作,这是我使用的原始指南。

http://www.youtube.com/watch?v=79ZQDzzOHLk

我的目标是在类里面编写一次这个程序,并让它在我决定调用它的任何 View Controller 上工作。

感谢@harsh.prasad 和一些额外的研究,除了将按钮链接到它之外,我已经设法让它发挥作用,达到我想要的效果。

所以更新这个问题,希望它能帮助其他人。

这是我做的:

我创建了一个 UIView 类并将其命名为 MenuOne。

MenuOne.h

#import <UIKit/UIKit.h>

@interface TFMenuOne : UIView {
// Pop Up Menu

IBOutlet UIScrollView *scrollView;
IBOutlet UIButton *openMenu;
int draw1;
IBOutlet UIButton *backButton;
}

// Pop Up Menu
- (IBAction)openMenu_clicked:(id)sender;

// Reset draw1 to 0
- (void) resetView: (id) sender;

@property (retain, nonatomic) IBOutlet UIScrollView *scrollView;

@end

MenuOne.m

#import "TFMenuOne.h"

@implementation TFMenuOne
@synthesize scrollView;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

draw1 = 0;

scrollView = [[UIScrollView alloc] init];
[scrollView setBackgroundColor:[UIColor whiteColor]];
[scrollView setFrame:CGRectMake(0, 315, 568, 5)];
[scrollView setContentSize:CGSizeMake(568, 5)];


backButton = [[UIButton alloc] init];
[backButton setBackgroundColor:[UIColor greenColor]];
backButton.frame = CGRectMake(224, 350, 120, 30);

openMenu = [[UIButton alloc] init];
[openMenu setBackgroundImage:[UIImage imageNamed:@"menu-button-@2.png"]
forState:UIControlStateNormal];
openMenu.adjustsImageWhenHighlighted = NO;
[openMenu addTarget:self
action:@selector(openMenu_clicked:)
forControlEvents:UIControlEventTouchUpInside];
openMenu.frame = CGRectMake(256, 269, 64, 46);


[self addSubview:scrollView];
[self addSubview:backButton];
[self addSubview:openMenu];

}
return self;
}

// Allow for touch even through transparent View class
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
for (UIView *view in self.subviews) {
if (!view.hidden && view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event])
return YES;
}
return NO;
}

- (void) resetView: (id) sender {
draw1 = 1;
[self openMenu_clicked:sender];
}

- (IBAction)openMenu_clicked:(id)sender {
if (draw1 == 0) {

draw1 = 1;

[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
scrollView.frame = CGRectMake(0, 260, 568, 60);
} completion:^(BOOL finished) {

}];

[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
openMenu.frame = CGRectMake(256, 214, 64, 46);
} completion:^(BOOL finished) {

}];

[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
backButton.frame = CGRectMake(224, 275, 120, 30);
} completion:^(BOOL finished) {

}];


} else {

draw1 = 0;

[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
scrollView.frame = CGRectMake(0, 315, 568, 5);
} completion:^(BOOL finished) {

}];

[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
openMenu.frame = CGRectMake(256, 269, 64, 46);
} completion:^(BOOL finished) {

}];

[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
backButton.frame = CGRectMake(224, 350, 120, 30);
} completion:^(BOOL finished) {

}];

}
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/

@end

经过反复试验,为了让这个 UIView 类出现在多个 ViewController 上,我在 View Controller 的 m 文件中这样调用 View 。我遇到的障碍是菜单会打开,但是当我离开 View Controller 转到另一个 View Controller 时,菜单将处于我离开时的状态,它不会重置回关闭状态。下面的代码涵盖了再次感谢@harsh.prasad。我还设法使菜单具有动画效果。

@interface TFMapViewController ()
{
TFMenuOne *menuView;
}

@end

@implementation TFMapViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[menuView resetView:nil];
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
menuView = [[TFMenuOne alloc] initWithFrame:CGRectMake(0, 51, 568, 320)];
[self.view addSubview:menuView];

}

- (void) viewDidAppear:(BOOL)animated
{

[UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
menuView.frame = CGRectMake(0, 0, 568, 320);
} completion:^(BOOL finished) {

}];

}


- (void) viewDidDisappear:(BOOL)animated
{
[menuView resetView:nil];
[UIView animateWithDuration:0.0 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
menuView.frame = CGRectMake(0, 51, 568, 320);
} completion:^(BOOL finished) {

}];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

// Allows for Exit button to work
- (IBAction)returned:(UIStoryboardSegue *)segue {

}


@end

最佳答案

如果您想要那种功能,我希望您可以使用 tabsController,否则在菜单 View 中创建一个幻灯片类,然后您可以根据需要在所有 View 中使用它。不会重写同一段代码。

编辑:假设您已经创建了一个类 CustomMenuView,它基本上创建了您的菜单 View 。所以现在你可以通过以下方式在每个你想使用它的 View Controller 中使用它:

CustomMenuView *menuView = [CustomMenuView alloc] initWithFrame:CGRectMake(0, 200, 320, 100);
menuView.<properties you want to pass> = <set properties here>;
[self.view addSubView:menuView];

这将使用自定义菜单设置 View ,然后您可以处理此菜单中的操作。

关于ios - 在 ViewController 中显示动画 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20676475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com