ios - 如何制作可点击的uiimage?

标签 ios objective-c uiimageview uiimage

我有一个菜单栏作为 uiimage。我想让这个菜单栏上的 5 个菜单可以点击并链接到另一个 View 。谁能告诉我该怎么做?我发现人们在 uiimage 上使用 uibutton,这对我来说很好吗?如何将菜单栏分成 5 block 。

感谢 rmaddy 和 Brent Royal-Gordon

我决定使用 uiimage 作为菜单栏背景并在其上添加 uibutton。我尝试这样的事情:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:@"MOVIE" ofType:@"MP4"]];

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayer];

moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.repeatMode = MPMovieRepeatModeOne;
moviePlayer.shouldAutoplay = YES;
moviePlayer.scalingMode = MPMovieScalingModeFill;
[moviePlayer.view setFrame:CGRectMake(0,0,1330,320)];

[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setContentSize:CGSizeMake(1330,320)];
[scrollView setScrollEnabled: YES];
[scrollView setShowsHorizontalScrollIndicator:NO];
[scrollView setShowsVerticalScrollIndicator:NO];

[scrollView addSubview:moviePlayer.view];
[self.view addSubview:scrollView];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:image.frame];   
UIImage *menubar = [UIImage imageNamed:@"BG.jpg"];
imageView = [[UIImageView alloc] initWithImage:menubar];
imageView.contentMode = UIViewContentModeScaleToFill;
imageView.frame = CGRectMake(0, 258, imageView.frame.size.width, 25);
[self.view addSubview:imageView];

[imageView setUserInteractionEnabled:YES];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];   
[btn setFrame:CGRectMake(0,258,30,25)];
[btn addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:@"HOME" forState:UIControlStateNormal];

[imageView addSubview:btn];

但是当我在模拟器上运行时,该按钮在屏幕上不可见。我做错什么了?

最佳答案

使用手势识别器是个坏主意,因为:

  • 它迫使您自己解释每个子菜单的触摸区域。
  • 它对 VoiceOver 是不透明的。
  • 当用户触摸按钮时,它不会显示突出显示。这些亮点有助于用户了解用户界面在做什么,因此它们非常有值(value)。

  • 您应该做的是在 Photoshop 等图形工具中将单个图像分成五个,并将这些切片中的每个切片设为 image。的 UIButton .您需要为这些图像设计突出显示的状态(当其中一个被按住时显示),但无论如何您都应该这样做。

    如果必须在代码中对图像进行切片,可以使用 UIGraphicsBeginImageContextWithOptions(size, NO, 0.0) 来实现。并使用 drawAtPoint: 绘制大图使用适当的偏移量来获得您想要的切片,但是在 99.99999% 的情况下,在您的大型、功能强大、插墙式电源的 Mac 上切片图像一次比每次在小型应用程序上运行时更有意义,速度慢,电池供电的 iOS 设备。

    关于ios - 如何制作可点击的uiimage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16630736/

    相关文章:

    ios - 如何以编程方式向以编程方式创建的 UIView 添加约束?

    javascript - event.touches 始终未定义

    ios - Swift-Access UILabels 在 UITableView 的动态单元格中

    ios - Objective C-从数组中字典的键值添加数据

    objective-c - 制作图片跳转IOS

    ios - 如何将图像从 Parse 加载到 UIImageView (iOS)

    ios - 使用 Grand Central Dispatch (GCD) 时获取 OSSpinLockLock

    ios - 我怎样才能在 ios 7 中获得 CLCircularRegion 中心值?

    ios - UIGraphicsGetCurrentContext 保存旋转状态

    iphone - SDWebimage 占位符中的事件指示器