简体   繁体   English

点击后如何对子类UIView执行操作?

[英]How do I perform an action on a subclassed UIView when tapped?

I have subclassed UIView and I want to be able to set a stroke colour around the view when it is tapped. 我已经将UIView子类化,并且希望能够在点击视图时在视图周围设置笔触颜色。

At the moment I have added a gesture recogniser to the view in my View Controller like so: 目前,我已经在视图控制器中向视图添加了手势识别器,如下所示:

    CJGGameSquare* gameSquare = [[CJGGameSquare alloc] initWithSquareSize:_squareSize andSquareNumber:i andSquareName:[squareNames objectAtIndex:i]];
    [_mainBoard addSubview:gameSquare];

    CGRect gameSquareFrame = {
        xPosition,
        yPosition,
        _squareSize,
        _squareSize
    };

    [gameSquare setFrame:gameSquareFrame];

UIGestureRecognizer* singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(highlightSquare:)];
[gameSquare addGestureRecognizer:singleFingerTap];

I've also implemented the highlightSquare method as follows: 我还实现了highlightSquare方法,如下所示:

-(void)highlightSquare:(UITapGestureRecognizer *)sender;
{
    UIView* square = sender.view;
    square.backgroundColor = [UIColor redColor];
}

This sets the background to red but I want instead to set a stroke colour or some other values that are unique to my CJGGameSquare object. CJGGameSquare背景设置为红色,但是我想改为设置笔触颜色或CJGGameSquare对象唯一的其他一些值。

If you already have a subclass override these methods: 如果您已有子类,请重写以下方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

Then you can recognize when your UIView is touched an draw accordingly. 然后,您可以识别出何时相应地触摸了UIView的绘制。

You could draw a border to view's layer or you could draw a rectangular path in the drawRect:(NSRect *) of your subclass. 您可以绘制边框以查看图层,也可以在子类的drawRect:(NSRect *)中绘制矩形路径。

For example: 例如:

#import <QuartzCore/QuartzCore.h>

view.layer.borderColor = [UIColor greenColor].CGColor;
view.layer.borderWidth = 5.0f;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在视图控制器中显示了一个UIView,如何检测用户何时在此视图外部轻敲? - I have a UIView presented in a view controller, how do I detect when the user tapped outside of this view? 如何隐藏UIView并使其在iOS屏幕上轻按某个区域时显示 - How do I hide UIView and make it appear when an area is tapped on screen in iOS 在AVPlayerViewController中点击播放按钮时执行操作 - Perform action when play button is tapped in AVPlayerViewController 点击时如何改变UIView的风格? - How to change the style of an UIView when it's tapped? iOS-用户滑动屏幕时如何执行操作? - iOS - How do I perform an action when the user swipes the screen? 仅当关闭特定的UIViewController时,如何执行操作? - How do I perform an action only when a specific UIViewController is dismissed? 按下键盘并轻按x时在UISearchBar上执行操作 - Perform action on UISearchBar when Keyboard is down and the x has been tapped 如何在简单的 UIView 动画上执行操作 - How to perform action on simple UIView Animation 点击消息泡泡时如何采取行动 - How to take an action when message bubble tapped 轻按动作时如何使音频静音? - How to mute a audio sound when an action is tapped?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM