Skip to content

Instantly share code, notes, and snippets.

@codetalks-new
Created July 2, 2015 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codetalks-new/2ddf20dacc5c93fee8b7 to your computer and use it in GitHub Desktop.
Save codetalks-new/2ddf20dacc5c93fee8b7 to your computer and use it in GitHub Desktop.
一个简单的 CommentView ,模仿网易新闻评论框
//
// CommentView.swift
// GuilinLegal
//
// Created by Haizhen Lee on 15/7/2.
// Copyright (c) 2015年 banxi1988 . All rights reserved.
//
import UIKit
func animationCurveToOptions(curve:UIViewAnimationCurve) -> UIViewAnimationOptions{
switch curve{
case .EaseInOut:
return UIViewAnimationOptions.CurveEaseInOut
case .EaseIn:
return UIViewAnimationOptions.CurveEaseIn
case .EaseOut:
return UIViewAnimationOptions.CurveEaseOut
case .Linear:
return UIViewAnimationOptions.CurveLinear
}
}
class CommentView : UIView,UITextViewDelegate{
@IBOutlet weak var cancelButton: UIButton!
@IBOutlet weak var submitButton: UIButton!
@IBOutlet weak var textView: UITextView!
let commentViewHeight: CGFloat = 160
override func awakeFromNib() {
super.awakeFromNib()
textView.delegate = self
submitButton.enabled = false
self.frame = CGRect(x: 0, y: screenHeight - commentViewHeight, width: screenWidth, height: commentViewHeight)
}
func textViewDidChange(textView: UITextView) {
submitButton.enabled = !textView.text.isEmpty
}
static func initFromNib() -> CommentView{
return UINib(nibName: "CommentView", bundle: nil).instantiateWithOwner(self, options: nil).first as! CommentView
}
func show(){
self.frame = CGRect(x: 0, y: screenHeight - commentViewHeight, width: screenWidth, height: commentViewHeight)
let keyWindow = UIApplication.sharedApplication().keyWindow
keyWindow?.addSubview(self)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardFrameWillChange:", name: UIKeyboardWillChangeFrameNotification, object: nil)
textView.becomeFirstResponder()
}
func keyboardFrameWillChange(notification:NSNotification){
NSLog("keyboardFrameWillChange \(notification)")
let userInfo = notification.userInfo!
let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey]!.doubleValue
let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey]!.integerValue
var options = UIViewAnimationOptions.TransitionFlipFromBottom
if let animCurve = UIViewAnimationCurve(rawValue: curve){
options = animationCurveToOptions(animCurve)
}
let keyboardEndFrame = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue()
UIView.animateWithDuration(0.25, delay: 0, options: options, animations: { () -> Void in
let keyboardY = keyboardEndFrame.origin.y
self.frame = CGRect(x: 0, y: keyboardY - self.commentViewHeight, width: screenWidth, height: screenHeight)
}, completion: { (finished) -> Void in
})
}
func dismiss(){
textView.resignFirstResponder()
removeFromSuperview()
}
@IBAction func onCancelPressed(sender: AnyObject) {
dismiss()
}
@IBAction func onSubmitPressed(sender: AnyObject) {
dismiss()
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="7706" systemVersion="14E26a" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB" customClass="CommentView" customModule="GuilinLegal" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="320" height="160"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="aS2-a3-hfZ">
<rect key="frame" x="8" y="8" width="30" height="30"/>
<state key="normal" title="取消">
<color key="titleColor" red="0.068509414790000001" green="0.068507365880000004" blue="0.068508528169999999" alpha="1" colorSpace="calibratedRGB"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="onCancelPressed:" destination="iN0-l3-epB" eventType="touchUpInside" id="Twb-jL-eEa"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="xTp-BU-lal">
<rect key="frame" x="282" y="8" width="30" height="30"/>
<state key="normal" title="发送">
<color key="titleColor" red="0.068509414790000001" green="0.068507365880000004" blue="0.068508528169999999" alpha="1" colorSpace="calibratedRGB"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="onSubmitPressed:" destination="iN0-l3-epB" eventType="touchUpInside" id="AUe-PY-hUa"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="写评论" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="aQp-H1-fcG">
<rect key="frame" x="139" y="12.5" width="51" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.068509414790000001" green="0.068507365880000004" blue="0.068508528169999999" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="9sx-5D-yby">
<rect key="frame" x="8" y="46" width="304" height="99"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
</subviews>
<color key="backgroundColor" white="0.96289620007554155" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="aS2-a3-hfZ" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leadingMargin" id="AJo-1z-grE"/>
<constraint firstAttribute="bottom" secondItem="9sx-5D-yby" secondAttribute="bottom" constant="15" id="I9d-w3-dmQ"/>
<constraint firstAttribute="trailingMargin" secondItem="9sx-5D-yby" secondAttribute="trailing" id="IZS-tk-Rkt"/>
<constraint firstItem="9sx-5D-yby" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leadingMargin" id="Kh8-2n-PlZ"/>
<constraint firstItem="xTp-BU-lal" firstAttribute="trailing" secondItem="iN0-l3-epB" secondAttribute="trailingMargin" id="R7T-bD-5ws"/>
<constraint firstAttribute="centerX" secondItem="aQp-H1-fcG" secondAttribute="centerX" constant="-4.5" id="TK1-OL-233"/>
<constraint firstItem="xTp-BU-lal" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" constant="8" id="dQo-Pp-gUt"/>
<constraint firstItem="aS2-a3-hfZ" firstAttribute="baseline" secondItem="aQp-H1-fcG" secondAttribute="baseline" id="eP6-G6-GEt"/>
<constraint firstItem="aS2-a3-hfZ" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" constant="8" id="fRY-CX-Moi"/>
<constraint firstItem="9sx-5D-yby" firstAttribute="top" secondItem="xTp-BU-lal" secondAttribute="bottom" constant="8" id="gwT-1u-cx2"/>
</constraints>
<nil key="simulatedStatusBarMetrics"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<connections>
<outlet property="cancelButton" destination="aS2-a3-hfZ" id="oLZ-aC-wfh"/>
<outlet property="submitButton" destination="xTp-BU-lal" id="1lC-uR-TUW"/>
<outlet property="textView" destination="9sx-5D-yby" id="fcp-I5-McV"/>
</connections>
</view>
</objects>
</document>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment