我正在尝试创建的屏幕截图:
注意:如果您决定运行项目,则无需查看下面的代码,因为我在下面发布的所有类都在Dropbox压缩项目文件中。
底部的每个方块都选择不同的颜色,并且有一个不可见的方块,用于选择绿色右侧的形状类型。在用户选择这些形状之一后,用户将能够在屏幕的某个部分进行绘制。
整个项目:https://www.dropbox.com/s/f9pr0wo0j74iw4a/XcodeSwiftProject99999.zip?dl=0
如果您运行项目,创建一个帐户,然后单击登录,然后单击其中一行,然后单击其中一个彩色框(红色和绿色方块),应用程序将崩溃并收到以下错误消息:
2016-03-11 17:06:43.580 finalProject2[11487:1058358] <UIView: 0x7faba1677710; frame = (0 0; 414 736); autoresize = W+H; gestureRecognizers = <NSArray: 0x7faba1658d90>; layer = <CALayer: 0x7faba16563f0>>'s window is not equal to <finalProject2.RowTableViewController: 0x7faba165a3c0>'s view's window!
File slot 1
File slot 2
File slot 3
File slot 4
File slot 5
File slot 6
File slot 7
File slot 8
File slot 9
File slot 10
File slot 11
File slot 12
2016-03-11 17:06:44.746 finalProject2[11487:1058358] Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UIAlertController: 0x7faba16b7d60>)
MainProject场景类:
import UIKit
weak var FirstFileNameTextField: UILabel!
enum ShapeType: String
{
case Line = "Line"
case Ellipse = "Ellipse"
case Rectangle = "Rectangle"
case FilledEllipse = "Filled Ellipse"
case FilledRectangle = "Filled Rectangle"
case Scribble = "Scribble"
}
let shapes: [ShapeType] = [ .Line, .Ellipse, .Rectangle, .FilledEllipse, .FilledRectangle, .Scribble ]
class MainProjectScene: UIViewController
{
var row: Row?
@IBAction func PressedSaveAs(sender: UIButton) //this is the save as function that I would like to know how to change
{
//1. Create the alert controller.
var alert = UIAlertController(title: "Name/rename your file:", message: "Enter a filename to name/rename and save your file", preferredStyle: .Alert)
//2. Add the text field. You can configure it however you need.
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.text = "Your file name"
})
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:
{
(action) -> Void in
let textField = alert.textFields![0] as UITextField
print("Text field: \(textField.text)")
// rows.cell.textLabel?.text = textField.text
CurrentFileName = textField.text!
rows[IndexPath.row].FileName = textField.text!
rows[IndexPath.row].UserText = self.TextUserScrollEdit.text!
}))
// 4. Present the alert.
self.presentViewController(alert, animated: true, completion: nil)
// rows[indexPath.row].FileName = rows.cell.textLabel?.text
// rows[i] = textField.text
// if let detailViewController = segue.destinationViewController as? MainProjectScene {
// if let cell = sender as? UITableViewCell {
// if let indexPath = self.tableView.indexPathForCell(cell) {
// detailViewController.row = rows[indexPath.row]
}
override func viewWillAppear(animated: Bool)
{
if let r = row
{
row!.FileName = r.FileName
row!.QuartzImage = r.QuartzImage
row!.UserText = r.UserText
rows[IndexPath.row].UserText = self.TextUserScrollEdit.text! }
}
override func viewDidLoad()
{
super.viewDidLoad()
TextUserScrollEdit.text = rows[IndexPath.row].UserText
// FacebookButton.addTarget(self, action: "didTapFacebook", forControlEvents: .TouchUpInside)
}
@IBOutlet weak var TextUserScrollEdit: UITextView!
@IBOutlet weak var NewFileButton: UIButton!
@IBOutlet weak var TwoDQuartzButton: UIButton!
@IBOutlet weak var YouTubeButton: UIButton!
@IBOutlet weak var TwitterButton: UIButton!
@IBOutlet weak var OpenFileButton: UIButton!
@IBOutlet weak var SnapChatButton: UIButton!
@IBOutlet weak var FacebookButton: UIButton!
@IBAction func PressedTwoDQuartzButton(sender: UIButton) {
}
@IBAction func PressedSnapchatButton(sender: UIButton){
UIApplication.sharedApplication().openURL(NSURL(string: "https://www.snapchat.com/")!)
}
@IBAction func PressedYouTubeButton(sender: UIButton) {
UIApplication.sharedApplication().openURL(NSURL(string: "https://www.youtube.com/")!)
}
@IBOutlet weak var InstagramButton: UIButton!
@IBAction func PressedFacebookButton(sender: UIButton) {
UIApplication.sharedApplication().openURL(NSURL(string: "http://www.facebook.com")!)
}
@IBAction func PressedInstagramButton(sender: UIButton) {
UIApplication.sharedApplication().openURL(NSURL(string: "https://www.instagram.com/")!)
}
@IBAction func PressedTwitterButton(sender: UIButton) {
UIApplication.sharedApplication().openURL(NSURL(string: "https://twitter.com/")!)
}
@IBOutlet weak var SaveAsButton: UIButton!
// @IBOutlet weak var shapeButton: ShapeButton!
@IBOutlet weak var canvas: CanvasView!
@IBOutlet var colorButtons: [UIButton]!
@IBOutlet weak var shapeButton: ShapeButton!
@IBAction func selectColor(sender: UIButton) {
UIView.animateWithDuration(0.5, delay: 0.0,
usingSpringWithDamping: CGFloat(0.25),
initialSpringVelocity: CGFloat(0.25),
options: UIViewAnimationOptions.CurveEaseInOut, animations: {
for button in self.colorButtons {
button.frame.origin.y = self.view.bounds.height - 58
}
sender.frame.origin.y -= 20
}, completion: nil)
canvas.color = sender.backgroundColor!
shapeButton.color = sender.backgroundColor!
}
@IBAction func selectShape(sender: ShapeButton) {
let title = "Select Shape"
let alertController = UIAlertController(title: title, message: nil, preferredStyle: .ActionSheet)
for shape in shapes {
let action = UIAlertAction(title: shape.rawValue, style: .Default) { action in
sender.shape = shape
self.canvas.shape = shape
}
alertController.addAction(action)
}
presentViewController(alertController, animated: true, completion: nil)
}
}
ShapeButton类:
import UIKit
class ShapeButton: UIButton {
let shapes: [ShapeType] = [ .Line, .Ellipse, .Rectangle, .FilledEllipse, .FilledRectangle, .Scribble ]
var shape: ShapeType = .Line {
didSet {
setNeedsDisplay()
}
}
var color: UIColor = UIColor.blueColor() {
didSet {
setNeedsDisplay()
}
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
let context = UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(context, color.CGColor)
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextSetLineWidth(context, 2)
let x1: CGFloat = 5
let y1: CGFloat = 5
let x2: CGFloat = frame.width - 5
let y2: CGFloat = frame.height - 5
let rect = CGRect(x: x1, y: y1 + 5, width: frame.width - 10, height: frame.height - 20)
switch shape {
case .Line:
CGContextMoveToPoint(context, x1, y1)
CGContextAddLineToPoint(context, x2, y2)
CGContextStrokePath(context)
case .Ellipse:
CGContextStrokeEllipseInRect(context, rect)
case .Rectangle:
CGContextStrokeRect(context, rect)
case .FilledEllipse:
CGContextFillEllipseInRect(context, rect)
case .FilledRectangle:
CGContextFillRect(context, rect)
case .Scribble:
CGContextMoveToPoint(context, x1, y1)
CGContextAddCurveToPoint(context,
x1 + 80, y1 - 10, // the 1st control point
x2 - 80, y2 + 10, // the 2nd control point
x2, y2) // the end point
CGContextStrokePath(context)
}
}
}
Canvas视图类:
import UIKit
/*
This program is for Xcode 6.3 and Swift 1.2
*/
class CanvasView: UIView {
var shape: ShapeType = .Line
var color: UIColor = UIColor.blueColor()
var first :CGPoint = CGPointZero
var last :CGPoint = CGPointZero
var points: [CGPoint] = []
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
let context = UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(context, color.CGColor)
CGContextSetFillColorWithColor(context, color.CGColor)
let rect = CGRect(x: first.x, y: first.y, width: last.x - first.x, height: last.y - first.y)
switch shape {
case .Line:
CGContextMoveToPoint(context, first.x, first.y)
CGContextAddLineToPoint(context, last.x, last.y)
CGContextStrokePath(context)
case .Ellipse:
CGContextStrokeEllipseInRect(context, rect)
case .Rectangle:
CGContextStrokeRect(context, rect)
case .FilledEllipse:
CGContextFillEllipseInRect(context, rect)
case .FilledRectangle:
CGContextFillRect(context, rect)
case .Scribble:
CGContextMoveToPoint(context, first.x, first.y)
for p in points {
CGContextAddLineToPoint(context, p.x, p.y)
}
CGContextStrokePath(context)
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
first = touch.locationInView(self)
last = first
points.removeAll(keepCapacity: true)
if shape == .Scribble {
points.append(first)
}
setNeedsDisplay()
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
last = touch.locationInView(self)
if shape == .Scribble {
points.append(last)
}
setNeedsDisplay()
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
last = touch.locationInView(self)
if shape == .Scribble {
points.append(last)
}
setNeedsDisplay()
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
}
}