Showing posts with label Make ios apps. Show all posts
Showing posts with label Make ios apps. Show all posts

Friday, February 22, 2019

Swift read poem app (2)

We want user can swipe to next poem in the list.
We will use GestureRecognizer to do.
Copy array poem name qua2 to reading class and declare 4 more arrays.
var qua2=["Bài một","Bài hai","Bài ba","Bài bốn","Bài năm","Bài sáu","Bài bảy","Bài tám"]
var ten2 = [String](count: 8, repeatedValue: " ")
var tacgia2 = [String](count: 8, repeatedValue: " ")
var ar2 = [String](count: 8, repeatedValue: " ")
var noidung = [String](count: 8, repeatedValue: " ")
Declare 2 variables.
var vi=0
var so2=0
Now we read all contents to array  noidung, one poem to one element.
Copy in to viewDidLoad
for i in0..<ar.count {
let path2 = NSBundle.mainBundle().pathForResource(ar[i], ofType: "txt")!
var rea2 : String = ""
var  t = ""
do {
try rea2 = NSString(contentsOfFile: path2, encoding: NSUTF8StringEncoding) asString
 t=rea2
noidung[i]=t
        }
catchlet error asNSError {
// print("ERROR : reading from file \(fileName) : \(error.localizedDescription)")
        }
        }
Now we need to convert array, choosen poem will move to top, others continue after.
Copy to above the line set text to textView.
for i in0..<tacgia.count {
so2=so+i
if(so2>7){
so2=so2-8
 }
ten2[i]=qua2[so2]
tacgia2[i]=tacgia[so2]
ar2[i]=noidung[so2]
 }
Change the line set text to textView like this.
gi.text = ar2[0]
Now create function to open next poem when user touch screen, copy to outside viewDidLoad.
func tap2 (g:UIGestureRecognizer) {
vi=vi+1
if(vi>7){
vi=vi-8
        }
gi.text = ar2[vi]
la.text = ten2[vi]
la2.text = tacgia2[vi]
 }
When user touch screen, we increase count variable 1 number and set text from next element in arrays.
Copy in to viewDidLoad.
let t3 = UITapGestureRecognizer(target:self, action:#selector(fo.tap2(_:)))
 t3.numberOfTapsRequired=2
view.addGestureRecognizer(t3)
If set numberOfTapsRequired=1 mean one touch, 2 mean double touch.
If you like user swipe to next poem, you must create a back button because in part 1 we swipe back to previous screen.
After that, change function in swipe command to tap2 like this.
let t2 = UIPanGestureRecognizer(target:self, action:#selector(fo.tap2(_:)))

view.addGestureRecognizer(t2)



Swift read poem app

Copy poems in notepad and save to txt file with UTF-8 Encoding.
Drags files in left tree folder project, check Copy Items is needed.
Declare a label, tableView, imageview, array poems ‘s name.
var tableView: UITableView!
var qua2=["Bài một","Bài hai","Bài ba","Bài bốn","Bài năm","Bài sáu","Bài bảy","Bài tám"]
let image = UIImage(named: "hoa")
var ima: UIImageView!
var la: UILabel!
Set location, color text, add to view in viewDidLoad.
let c = UIScreen.mainScreen().bounds.size.height
la = UILabel(frame: CGRect(x: 120, y: 10, width: 200, height: 30))
let t = "My poems"
let a=NSMutableAttributedString(string: t, attributes: [NSForegroundColorAttributeName: UIColor.orangeColor(), NSFontAttributeName: UIFont(name: "Georgia", size: 18.0)!])
la.attributedText = a
tableView = UITableView(frame: CGRect(x: 10, y: 70, width: 325, height: c-50))
let the = tableView
        the.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "iden")
        the.dataSource = self
        the.delegate = self
view.backgroundColor = UIColor.whiteColor()
view.addSubview(la)
view.addSubview(the)
Copy function to create tableView to outside viewDidLoad.
func numberOfSections(in tableView: UITableView) ->Int {
return1
    }
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int {
return qua2.count
    }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("iden", forIndexPath: indexPath)
        cell.textLabel?.text = qua2[indexPath.row]
        cell.textLabel?.font = UIFont(name:"Futura", size:18)
        cell.textLabel?.textColor=UIColor.blueColor()
        cell.imageView?.image = image
        cell.accessoryType = .DisclosureIndicator
return cell
    }
func tableView(tableView: UITableView,
                   didSelectRowAtIndexPath indexPath: NSIndexPath) {
let row = qua2[indexPath.row]
let so = indexPath.row
let vc = fo()
        vc.so=so
        vc.tenbai=row
self.presentViewController(vc, animated: true, completion: nil)
    }
See last function, we pass poems ‘s name and number line to reading class.
Copy this function to remove status bar.
Override  func prefersStatusBarHidden() ->Bool {
return true
    }
Run to see result.
Create new class to read text, name it fo.
Declare 2 label to show name of poem, author, 1 textView to show poem, imageview to set background, 2 variable to receive data pass through.
var so=0
var tenbai = " "
let im = UIImage(named: "nen")
var la: UILabel!
var la2: UILabel!
var ima: UIImageView!
var gi:UITextView!
var ar = ["t1","t2","t3","t4","t5","t6","t7","t8"]
var tacgia = ["Teika","Jakuren","Komachi","Saigyo","Ise","Shunzei","Shotetsu","Sosei"]
Set location in viewDidLoad.
let c = UIScreen.mainScreen().bounds.size.height
let r = UIScreen.mainScreen().bounds.size.width
la = UILabel(frame: CGRect(x: 80, y: 80, width: 100, height: 30))
la2 = UILabel(frame: CGRect(x: 140, y: 250, width: 100, height: 30))
la.text = tenbai
la2.text = tacgia[so]
la2.textColor = UIColor.darkGrayColor()
la.textColor = UIColor.magentaColor()
ima = UIImageView(frame: CGRect(x: 0, y: 0, width: r, height: c))
ima.image = im
gi = UITextView(frame: CGRect(x:50, y: 120, width: 250, height: 160))
view.backgroundColor = UIColor.whiteColor()
la.center = CGPoint(x: view.center.x, y:90)
We add imageview to make it become background picture.
view.addSubview(ima)
view.addSubview(gi)
view.addSubview(la)
view.addSubview(la2)
Copy these lines to read file txt in to viewDidLoad.
let path2 = NSBundle.mainBundle().pathForResource(ar[so], ofType: "txt")!
var rea2 : String = ""
var  t = ""
do {
try rea2 = NSString(contentsOfFile: path2, encoding: NSUTF8StringEncoding) asString
            t=rea2
   }
catchlet error asNSError {
// print("ERROR : reading from file \(fileName) : \(error.localizedDescription)")
        }
gi.text = t
gi.font = UIFont.italicSystemFontOfSize(17)
gi.textColor=UIColor.blueColor()
gi.textAlignment = NSTextAlignment.Center
gi.backgroundColor=UIColor.clearColor()
We set text to textView, color text, make it transparent so we can see background picture.
Now we want to swipe back to previous screen, copy to outside viewDidLoad.
func tap (g:UIGestureRecognizer) {
self.dismissViewControllerAnimated(true, completion: nil)
}
In side viewDidLoad add these lines.
let t2 = UIPanGestureRecognizer(target:self, action:#selector(fo.tap(_:)))
view.addGestureRecognizer(t2)

Run to see result.