IOS考试题2

IOS考试题2

在这里插入图片描述

考点,考察UI控件的坐标获取,动画的使用等知识的使用,OC能写出来,swift就能写出来,swiftUI略过,国内使用率10%(截止今天)
oc写法

//
//  ViewController.m
//  002-考试题2oc1
//
//  Created by 鲁军 on 2021/5/30.
//

#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *itcastButton;
@property (weak, nonatomic) IBOutlet UIView *underline;
@property(nonatomic,weak)UIButton *selectedButton;
@end
@implementation ViewController
- (IBAction)buttonClick:(UIButton *)sender {
    // NSLog(@"%@",sender.currentTitle);
    if(self.selectedButton!=nil){
        self.selectedButton.selected = NO;
    }
    sender.selected = YES;
    self.selectedButton = sender;
    [UIView animateWithDuration:0.25 animations:^{
        CGFloat underlineX = CGRectGetMinX(sender.frame);
        CGFloat underlineY = CGRectGetMaxY(sender.frame);
        CGFloat underlineW = sender.frame.size.width;
        CGFloat underlineH = self.underline.frame.size.height;
        self.underline.frame = CGRectMake(underlineX, underlineY, underlineW, underlineH);
       }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated{
    [UIView setAnimationsEnabled:NO];
    [self buttonClick:self.itcastButton];
    [UIView setAnimationsEnabled:YES];
}
@end

swift写法

//
//  ViewController.swift
//  02-考试题2
//
//  Created by 鲁军 on 2021/5/25.
//
import UIKit
class ViewController: UIViewController {
    private var selectedButton: UIButton?
    @IBOutlet weak var underline: UIView!
    @IBOutlet weak var itcastButton: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
    }
   
    @IBAction func buttonClick(sender: UIButton) {
        //debugPrint(sender.currentTitle as Any)
           
        if (selectedButton != nil) {
                selectedButton!.isSelected = false
            }
            sender.isSelected = true
            self.selectedButton = sender
            UIView.animate(withDuration: 0.25, animations: {
                let underlineX = sender.frame.minX
                let underlineY = sender.frame.maxY
                let underlineW = sender.frame.width
                let underlineH = self.underline.frame.height
            self.underline.frame = CGRect(x: underlineX, y: underlineY, width: underlineW, height: underlineH)
        })
    }
    override func viewDidAppear(_ animated: Bool)  {
        UIView.setAnimationsEnabled(false)
        buttonClick(sender: self.itcastButton)
        UIView.setAnimationsEnabled(true)
    }
}


相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...