*1> 简单实现列表折叠效果
extension ViewController : UITableViewDelegate ,UITableViewDataSource{
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 1
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionView = UIView(frame: CGRect(x: 0, y: 0, width: screen_width, height: 44))
sectionView.backgroundColor = UIColor.cyan
sectionView.isUserInteractionEnabled = true
let label = UILabel(frame: CGRect(x: 20, y: 0, width: 100, height: 44))
label.text = dataArr[section]
sectionView.addSubview(label)
sectionView.tag = section
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.display1(tap:)))
sectionView.addGestureRecognizer(tap)
return sectionView
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let str = "\(section)"
guard let value = dataDict[str] else {
dataDict[str] = "0"
return 0
}
if (value as NSString).intValue == 1 {
return 5
}else{
return 0
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44
}
func numberOfSections(in tableView: UITableView) -> Int {
return dataArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = "每一行"
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
print("\(indexPath.section) \(indexPath.row)")
}
}
extension ViewController{
@objc fileprivate func display1(tap : UITapGestureRecognizer){
let str = "\(tap.view!.tag)"
let indexSet = NSIndexSet(index: Int((str as NSString).intValue))
guard let value = dataDict[str] else {
dataDict[str] = "1"
tableView?.beginUpdates()
tableView?.reloadSections(indexSet as IndexSet, with: .automatic)
tableView?.endUpdates()
return
}
if (value as NSString).intValue == 0 {
dataDict[str] = "1"
}else{
dataDict[str] = "0"
}
tableView?.beginUpdates()
tableView?.reloadSections(indexSet as IndexSet, with: .automatic)
tableView?.endUpdates()
}
}
* 2. 图片的放大缩小,移动
// 捏合手势
func didPinch(_ pinch: UIPinchGestureRecognizer) {
if pinch.state != .changed {
return
}
let scale = pinch.scale
let location = pinch.location(in: view)
let scaleTransform = imageV.transform.scaledBy(x: scale, y: scale)
imageV.transform = scaleTransform
let dx = imageV.frame.midX - location.x
let dy = imageV.frame.midY - location.y
let x = dx * scale - dx
let y = dy * scale - dy
// 这样的话计算会有错误
//imageView.transform = imageView.transform.translatedBy(x: x, y: y);
let translationTransform = CGAffineTransform(translationX: x, y: y)
imageV.transform = imageV.transform.concatenating(translationTransform)
pinch.scale = 1
}
// 利用Pan手势实现拖拽
func didPan(_ pan: UIPanGestureRecognizer) {
if pan.state != .changed {
return
}
let scale = imageV.frame.size.width / UIScreen.main.bounds.size.width
print(scale)
let translation = pan.translation(in: view)
let transform = imageV.transform.translatedBy(x: translation.x / scale, y: translation.y / scale)
imageV.transform = transform
pan.setTranslation(.zero, in: view)
}
* 3. 购买详情动画
func transformAnimation() {
var transform = CATransform3DIdentity
// 视图角度
transform.m34 = -1.0 / 2000;
// 尺寸缩小(transform对象,X轴,Y轴,Z轴)
transform = CATransform3DScale(transform, 1, 1, 1)
// 沿某轴旋转(transform对象,旋转角度,X轴,Y轴,Z轴)
let angel = 15.0 * (CGFloat)(M_PI) / 180.0
transform = CATransform3DRotate(transform, angel, 1, 0, 0)
// 第二次 变形实在第一次的基础上
// transform.m34 = transform.m34
// 沿着某轴移动(transform对象,X轴,Y轴,Z轴)
transform = CATransform3DTranslate(transform, 0, 10, 0)
// 尺寸缩小(transform对象,X轴,Y轴,Z轴)
transform = CATransform3DScale(transform, ( UIScreen.main.bounds.size.width-40)/UIScreen.main.bounds.size.width, ( UIScreen.main.bounds.size.height-20)/UIScreen.main.bounds.size.height, 1)
self.view.layer.transform = transform
}