点击时显示 UiImageView
我想添加一个默认情况下应隐藏的图像视图,当用户点击时 它的位置,图像应该出现。
例如:
如果有一块巧克力
,我在左下角的顶部添加了一个小的 UIImageView 并将其设置为隐藏, 当用户点击那块巧克力时,新的小图像视图应该出现并覆盖 那个被攻丝的部分。
这是我的代码
.h 文件
#import <UIKit/UIKit.h>
@interface ChompViewController : UIViewController {
int s;
int turn;
IBOutlet UIImageView *sq1;
}
//@property (nonatomic, retain) IBOutlet UIImageView *aq1;
- (IBAction) mainMenu:(id)sender;
@end
.m 文件:
#import "ChompViewController.h"
#import "MainMenuViewController.h"
@implementation ChompViewController
// Main Menu button in Start Game view.
- (IBAction) mainMenu:(id)sender {
MainMenuViewController* mainM = [[MainMenuViewController
alloc] initWithNibName:@"MainMenuViewController" bundle:nil];
[self.navigationController pushViewController:mainM animated:YES];
}
// The Game Method.
- (void) startGame {
s=20;
while (s>0) {
// Players' Turn
if (turn = 1) {
turn = 0;
}
// Computers' Turn
else {
turn = 1;
}
}
}
@end
所以我想在下面的两个“if”语句中实现点击和隐藏
- (void) StartGame
我希望你能帮助我,我真的需要你的帮助..
谢谢
编辑: 我找到了一种方法并做了一些更改并且它有效,但是在点击时它有效 屏幕上的任何位置,我希望当用户点击小 UIImageView 的确切位置时触发它。
另外,我不知道如何在 StartGame 方法中实现此方法。
代码
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
sq1.hidden = NO;
[sq1 release];
}
这是我知道我问了很多的
,我是菜鸟,需要一些帮助来提高我的 iPhone 开发知识。 ===============
编辑2..
我做了别的事情,但是代码中有问题,我不知道它是什么..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
s=20;
turn =1;
while (s>0) {
// Players' Turn
if (turn = 1) {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(sq1.frame, touchLocation)){
sq1.hidden = YES;
}
turn = 0;
}
// Computers' Turn
else {
//Computer Here
turn = 1;
}
}
}
现在当我触摸imageView时,模拟器冻结并退出应用程序。
I want to add an image view that should be hidden by default, and when the user taps
its location, the image should appear.
For example:
If there is a chocolate bar
and I added a small UIImageView on top of the left bottom piece and set it to be hidden,
when the user taps that piece of chocolate, the new small image view should come and cover
that tapped piece.
This is the code I have
The .h file
#import <UIKit/UIKit.h>
@interface ChompViewController : UIViewController {
int s;
int turn;
IBOutlet UIImageView *sq1;
}
//@property (nonatomic, retain) IBOutlet UIImageView *aq1;
- (IBAction) mainMenu:(id)sender;
@end
The .m file:
#import "ChompViewController.h"
#import "MainMenuViewController.h"
@implementation ChompViewController
// Main Menu button in Start Game view.
- (IBAction) mainMenu:(id)sender {
MainMenuViewController* mainM = [[MainMenuViewController
alloc] initWithNibName:@"MainMenuViewController" bundle:nil];
[self.navigationController pushViewController:mainM animated:YES];
}
// The Game Method.
- (void) startGame {
s=20;
while (s>0) {
// Players' Turn
if (turn = 1) {
turn = 0;
}
// Computers' Turn
else {
turn = 1;
}
}
}
@end
So I want to implement the tap and hide in the two 'if' statements under
- (void) StartGame
I hope you can help me, I really need your help..
Thanks
EDIT:
I have found a method and made some changes and it works, but it works when tapping
any location on the screen and I want it to be triggered when the user taps the exact location of the small UIImageView.
Also, I do not know how to implement this method inside my StartGame method.
This is the code
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
sq1.hidden = NO;
[sq1 release];
}
I know I am asking a lot, I am noob and need some help to improve my knowledge of iPhone development.
===============
EDIT 2..
I have done something else,, but there is something wrong in the code and I do not know what is it..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
s=20;
turn =1;
while (s>0) {
// Players' Turn
if (turn = 1) {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(sq1.frame, touchLocation)){
sq1.hidden = YES;
}
turn = 0;
}
// Computers' Turn
else {
//Computer Here
turn = 1;
}
}
}
Now when I touch the imageView, the simulator freezes and quits the app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是在隐藏的 UIImageView 位置顶部放置一个没有图像和标题文本(因此它是透明的)的自定义 UIButton,并将按钮的 TouchUpInside 操作连接到执行以下操作的方法:
Simplest is to place a custom UIButton with no image and no title text (so it's transparent) on top of the hidden UIImageView location and connect the TouchUpInside action of the button to a method that performs
现在我可以想到两种可能的方法来做到这一点(可能还有更多):
使用touchesStarted: withEvent: 方法并检查触摸是否在观察区域内。如果是,则显示/取消隐藏 UIImageView
忘记 UIImageView 并使用透明 UIButtons 代替。当按钮被按下时,如果 UIButton 被按下,则将图像添加到实例中。
Right now I can think of two possible ways to do this (probably there are more):
Use the touchesStarted: withEvent: Method and check whether the touch was inside the observed area. If it was, display/unhide the UIImageView
Forget the UIImageView and use transparent UIButtons instead. When the button gets pressed add the image to the instance if UIButton that was pressed.