[英]Add image to subclassed UIImageView
I am trying to add a tap gesture from a subclassed UIImageView and then control the tap from the View Controller. 我试图从子类UIImageView添加轻击手势,然后从视图控制器控制轻击。 I am not getting any compiling errors but "addSubview" is not displaying any image.
我没有任何编译错误,但“ addSubview”未显示任何图像。 How can make the UIImageView to be displayed?
如何使UIImageView显示?
If I try to control the tap and pan gestures from the subclassed UIImageVIew I have no problems but I would like to control these functions from the View Controller 如果我尝试通过子类UIImageVIew控制点击和平移手势,则没有问题,但是我想从View Controller中控制这些功能。
Relevant code looks like this. 相关代码如下所示。
UIImageView subclass UIImageView子类
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
//self.userInteractionEnabled = YES;
previewController = [[PreviewController alloc]init];
[previewController self];
[self addSubview:character];
// Tap Initialization code
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:(PreviewController *)self.previewController action:@selector(addCharacter:)];
[self addGestureRecognizer:tap];
self.userInteractionEnabled = YES;
}
return self;
}
View Controller 查看控制器
- (void)addCharacter:(UITapGestureRecognizer *)t
{
NSLog(@"add character");
imageNSArray = [NSMutableArray array];
uiImg = [UIImage imageNamed:@"homer.png"];
CGPoint loc = [t locationInView:self.view];
character = [[UIImageView alloc] initWithImage:uiImg];
character.center = loc;
[imageNSArray addObject:character];
//Locate the imageNSArray on frameImageView area only.
[self.view addSubview:character];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panCharacter:)];
[self.character addGestureRecognizer:pan];
character.userInteractionEnabled = YES;
}
put a view behind your subclassed imageView. 在子类imageView后面放置一个视图。 Apply the gestureRecognition to the new View.
将手势识别应用于新视图。 You can control your tap gesture from new view.
您可以从新视图控制点击手势。 Let me know if i am not clear, or if more info needed.
让我知道是否不清楚,或者是否需要更多信息。
I think what you want is.. .you want to show a image when user taps on screen. 我想您想要的是..您希望在用户点击屏幕时显示图像。 Do following steps: 1. drag & drop a tap gesture recognizer on default view of your view controller.
请执行以下步骤:1.将轻击手势识别器拖放到视图控制器的默认视图上。 2. connect (ctrl +) gestureRecognizer with ViewController.m
2.将(ctrl +)手势识别器与ViewController.m连接
Take a look at this code. 看一下这段代码。 (Just modified your code)
(只需修改您的代码)
- (IBAction)onTap:(UITapGestureRecognizer *)sender {
NSLog(@"add character");
UIImage * uiImg = [UIImage imageNamed:@"sel.png"];
CGPoint loc = [sender locationInView:self.view];
UIImageView * character = [[UIImageView alloc] initWithImage:uiImg];
character.center = loc;
[self.view addSubview:character];
}
Let me know if this is not what you want… 让我知道这是否不是您想要的...
Edit better go for this code… No need to do above steps. 最好编辑此代码……无需执行上述步骤。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
[self.view addGestureRecognizer:tap];
self.view.userInteractionEnabled = YES;
}
- (void)onTap:(UITapGestureRecognizer *)sender
{
NSLog(@"add character");
UIImage * uiImg = [UIImage imageNamed:@"sel.png"];
CGPoint loc = [sender locationInView:self.view];
UIImageView * character = [[UIImageView alloc] initWithImage:uiImg];
character.center = loc;
[self.view addSubview:character];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.