IOS从零开发一款App(四)(UIView的手势点击监听)

本文介绍了如何在iOS应用中添加UIButton并设置不同状态的文字显示及背景颜色,同时讲解了UIButton事件监听的实现方法。此外,还展示了如何为UIView添加手势监听,并提供了使用WKWebView加载外部网页的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.添加 UIButton

   [self.contentView addSubview:({
            self.button = [[UIButton alloc]initWithFrame:CGRectMake(200, 80, 20, 20)];
//            self.rightImgView.contentMode = UIViewContentModeScaleAspectFit;
            [self.button setTitle:@"k" forState:UIControlStateNormal];
            [self.button setTitle:@"M" forState:UIControlStateHighlighted];
            self.button.backgroundColor =[UIColor redColor];
            self.button;
        })];

2. UIButton 的事件监听

image-20210501002213633
image-20210501003544260

3. UIView 的手势监听

给UIView 注册 UITapGestureRecognizer 的手势监听

  //添加一个UIView ,设置UIView的手势监听
        UIView *gestureView = [[UIView alloc]initWithFrame:CGRectMake(200, 200, 200, 200)];
        gestureView.backgroundColor=[UIColor yellowColor];
        UITapGestureRecognizer *recognize = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickMyCustomView)];
        [gestureView addGestureRecognizer:recognize];

4. WebView 基础

image-20210501215900358
实现新闻页跳转到对应的网页

  • 新建DetailViewControl.m
#import "DetailViewController.h"
#import <WebKit/WebKit.h>

@interface DetailViewController ()

@property(nonatomic,strong,readwrite) WKWebView *webView;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 88, self.view.frame.size.width, self.view.frame.size.height-88)];
    
    [self.view addSubview: self.webView];
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://time.geekbang.org"]]];
}
  • 效果图
    image-20210501222859497
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小菜的OnePiece

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值