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)
}
}