Skip to content

Instantly share code, notes, and snippets.

@Haeuncs
Last active December 13, 2019 10:35
Show Gist options
  • Save Haeuncs/f46595c5b855b726528099c8bab54e39 to your computer and use it in GitHub Desktop.
Save Haeuncs/f46595c5b855b726528099c8bab54e39 to your computer and use it in GitHub Desktop.
Scroll view moving to the center of a specific Button
//
// ViewController.swift
// ButtonsAnimate
//
// Created by LEE HAEUN on 2019/12/12.
// Copyright © 2019 LEE HAEUN. All rights reserved.
//
//코드 설명
//submit 버튼을 누르면 scrollview가 false인 버튼을 찾아서 그 중앙으로 이동하는 소스
//만약 scrollview의 프레임 중앙보다 작은 값일 때도 false 인 버튼이 중앙으로 올 수 있도록
//비어있는 UIView의 topViewHeight의 height을 조절해서 중앙으로 올 수 있게 함
//scrollview의 y 위치가 비어있는 UIView 에 값보다 커지면 height을 0으로 줘 원 상태로 바꾼다.
//또한 tag 를 지정해서 tag값으로 button 을 찾아 그 button 의 midY 를 구한다.
import UIKit
class ViewController: UIViewController {
var topViewHeight: NSLayoutConstraint?
var check: [Bool] = [false,false,false,false]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
layout()
}
func layout() {
view.addSubview(scrollView)
scrollView.addSubview(stackView)
stackView.addArrangedSubview(topView)
stackView.addArrangedSubview(button0)
stackView.addArrangedSubview(button1)
stackView.addArrangedSubview(button2)
stackView.addArrangedSubview(button3)
stackView.addArrangedSubview(submit)
topViewHeight = topView.heightAnchor.constraint(equalToConstant: 0)
topViewHeight?.isActive = true
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.leftAnchor.constraint(equalTo: view.leftAnchor),
scrollView.rightAnchor.constraint(equalTo: view.rightAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor),
stackView.leftAnchor.constraint(equalTo: view.leftAnchor),
stackView.rightAnchor.constraint(equalTo: view.rightAnchor),
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
button0.heightAnchor.constraint(equalToConstant: 300),
button1.heightAnchor.constraint(equalToConstant: 300),
button2.heightAnchor.constraint(equalToConstant: 300),
button3.heightAnchor.constraint(equalToConstant: 300),
submit.heightAnchor.constraint(equalToConstant: 300),
])
}
lazy var scrollView: UIScrollView = {
let scroll = UIScrollView()
scroll.translatesAutoresizingMaskIntoConstraints = false
scroll.backgroundColor = .white
scroll.delegate = self
return scroll
}()
lazy var stackView: UIStackView = {
let stack = UIStackView()
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .vertical
return stack
}()
lazy var topView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
lazy var button0: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle(String(check[0]), for: .normal)
btn.tag = 1
btn.addTarget(self, action: #selector(buttonEvent), for: .touchUpInside)
btn.backgroundColor = .red
return btn
}()
lazy var button1: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle(String(check[1]), for: .normal)
btn.tag = 2
btn.addTarget(self, action: #selector(buttonEvent), for: .touchUpInside)
btn.backgroundColor = .orange
return btn
}()
lazy var button2: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle(String(check[2]), for: .normal)
btn.tag = 3
btn.addTarget(self, action: #selector(buttonEvent), for: .touchUpInside)
btn.backgroundColor = .green
return btn
}()
lazy var button3: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle(String(check[3]), for: .normal)
btn.tag = 4
btn.addTarget(self, action: #selector(buttonEvent), for: .touchUpInside)
btn.backgroundColor = .purple
return btn
}()
lazy var submit: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle("submit", for: .normal)
btn.addTarget(self, action: #selector(submitEvent), for: .touchUpInside)
btn.backgroundColor = .black
return btn
}()
}
extension ViewController {
@objc func buttonEvent(sender: UIButton) {
print(check)
check[sender.tag - 1] = !check[sender.tag - 1]
print(check)
sender.setTitle(String(check[sender.tag - 1]), for: .normal)
}
@objc func submitEvent(){
// print(button0.frame.midY)
// print(button1.frame.midY)
// print(button2.frame.midY)
// print(button3.frame.midY)
for i in 0..<check.count {
print("index: \(i)")
if !check[i]{
if let falseButton = self.view.viewWithTag(i + 1) as? UIButton {
print("스크롤뷰 전체 크기\(scrollView.contentSize.height)")
print("중앙값 \(falseButton.frame.midY)")
moveOffset(yPoint: CGPoint(x: 0, y: Int(falseButton.frame.midY)))
break
}
}
}
}
func moveOffset(yPoint: CGPoint){
print(scrollView.frame.height)
print(yPoint.y)
print((self.scrollView.frame.height/2) - yPoint.y)
if yPoint.y < (scrollView.frame.height/2) {
self.topViewHeight?.constant = (self.scrollView.frame.height/2) - yPoint.y
UIView.animate(withDuration: 0.33) {
self.scrollView.contentOffset = .zero
self.view.layoutIfNeeded()
}
}else{
UIView.animate(withDuration: 0.33) {
let y = (self.scrollView.frame.height/2) - yPoint.y
self.scrollView.contentOffset = CGPoint(x: 0, y: y < -1 ? y*(-1):y)
UIView.animate(withDuration: 0.33) {
self.view.layoutIfNeeded()
}
}
}
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y > topViewHeight!.constant {
topViewHeight?.constant = 0
}
}
}
@KimTH94
Copy link

KimTH94 commented Dec 13, 2019

우와 ㅋㅋㅋㅋ 아이폰데브에 글 올렸던 유저인데... 감사합니다!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment