Thursday, March 28, 2024

Scope Not Shown When Search Bar Is Activated

Recently, I noticed a minor issue after updating iOS. The scope bar did not appear when the search bar was activated. Typically, I prefer to display the scope bar simultaneously with the search bar activation. This allows users to select a scope item before searching.

An Example Case

Here is a snapshot. When I pull down the screen, only the search bar is displayed, but the scope bar is missing.


The scope bar appears when text is entered into the search bar. However, if a user selects a scope item, the search text disappears. This means that the user has to re-enter text if they selecte a different scope item. This is very inconvenient.



Solution

I spent some time thinking about the issue before diving into my project. It became apparent that there were some logic or behavior changes in iOS. Soon, I discovered that a property of the search bar had been deprecated and a new property was recommended as its replacement.

  1. private func configSearchControll() {
  2.  let sc =  UISearchController(searchResultsController: nil)
  3.  // config search controller
  4.  if #available(iOS 16.0, *) {
  5.        sc.scopeBarActivation = .onSearchActivation
  6.    } else {
  7.        if #available(iOS 13.0, *) {
  8.            sc.automaticallyShowsScopeBar = true
  9.        } else {
  10.            // Fallback on earlier versions
  11.        }
  12.    }
  13. }

I noticed that the deprecated property of automaticallyShowsScopeBar is a boolean type with only one choice. With the iOS update, the recommented property of scopeBarActivation is an enum type with several options. After further testing, I found that only the enumerator onSearchActivation is meeting my requirements.


The above method is called in the event of viewDidLoad.

  1. override func viewDidLoad() {
  2.  super.viewDidLoad()
  3.  // some configuration codes
  4.  DispatchQueue.main.async {
  5.    [weak self] in
  6.      guard let this = self else { return }
  7.      this.configSearchController()
  8.  }
  9. }

With above code updated, my iOS app TapMyNotes now works as expected!


I'll update my app TapMyNotes on the App Store with this bug-fix, as well as other new and exiting features, soon!

References

Read More...

Wednesday, February 21, 2024

Reveal Liu Qian's Magic Card Game

After Liu Qian's magic show at the Springs Festival Gala 2024, most people who participated in the game got expected results, but Xiao Ni (show anchor) did not make it. At that time, I thought this was a game with great possibilities for success. Soon, some people resolved the game and pointed out that this could be explained by a mathematical solution or method. Then I did further study on the game and figured out that the expected result is a definitive result, not a possible result. 

My Analyze Method

This magic game contains two mathematical methodologies. In order to reveal the game's logic, I use the circle-list method and display the card faces on a table. You will easily see and understand the mathematical logic in the game.

Take a piece of paper and divide it into four equal-sized cards. Draw four different pictures on the cards and label the cards on the left and right sides with A, B, C, and D.

Wash the cards well. Fold a line in the middle.

Tear the cards into halves based on the fold line. Each half has four cards.

Put one half of four cards on top of another half. Now you have a deck of eight cards.

In order to better explain, keep all cards face up and place them clockwise in a circle. Use a piece of green-coloured paper to mark the tail. The next card, clockwise, is the head.

As an example, use the D card as the tail. The head is the next card, clockwise, A. After A, the sequence of cards is B, C,...

With the cards ready, let's start to explain, focusing on two mathematical methods, the logic of the game.

Math One: Repliated Sequence

The first math used in the game is to arrange the cards in a replicated sequence.

As shown in the above picture, we can see that no matter where the start or head card is, the sequence of remaining cards follows a common rule: the first four cards are replicated in the second four cards in order. The following are some examples:
D as head, the order is D-A-B-C D-A-B-C
A as head, the order is A-B-C-D A-B-C-D
B as head, the order is B-C-D-A B-C-D-A
and more

No matter how you wash your cards at the beginning or how many times you place the head card at the bottom of the tail card, This sequence follows a similar replicated sequence.

Another very interesting feature is that taking the first three cards, the first half card, and the last one of the remaining five cards is the matched card, i.e., the original C card. For example, taking three cards from the head D card, the head and tail of the remaining cards are all from the C card.

Based on those math discoveries, the next step in the magic game is to take the first three cards out and place them between any remaining cards. For example, place the first three cards in any position of the organ arrows shown in the following picture.

Choose a position, as shown in the following picture, between B and C.

After inserting, the following is the case of eight cards in a sequence.

Now the head and tail are all C. Take the head C, the secret card, out, and place it aside.

The number of remaining cards is seven. Place the secret card in the middle of the circle.

Please note that the tail card is another C, a key card. At this moment, the other cards above the key are not important. Their sequence can be anything. As in the magic show, you can ask the audience to place those non-key cards anywhere, but keep the key at the end. For you, the magician, you should clearly remember that the last card is the matching card to the secret card.

Math Two: Josephus Problem

Another math in the magic show is the famouse Josephus math problem.

This is an old story from an ancient war. The historian Josephus and a team of soldiers were in a trap and had no way to escape. To avoid being captured and killed, they chose to commit suicide by each other. Then they designed a method, either by luck or the hand of God. They stood in a circle, one starting to kill the next one clockwise, and the next remaining one repeated the kill until the last one killed himself. The last remaining one could, in fact, survive.

The story became a math problem. N soldiers in a circle list. Start at one clockwise to count, the odd number remains, and even one out. Repeat this process until there is only one left. The math problem is to find out the index position of the surviving soldier. The problem is a very interesting pure math and computer science problem.

Liu Qian's game was designed based on this math problem. For a game with five or six cards in a list, Liu organised several deceiving steps to find a way to move the last matching card to the surviving position so that he could make the perfect match. Coincidentally, the seven-charcter magic word can move the last card in the 5 or 6 card list to the survive position! Interstingly, I found out that the same step applies to a seven-card list.

Continue the game based on the result of the seven cards in the above picture. Throw the head-A card away. This is the result:

Read out aloud the seven-letter magic word one by one and move the head card to the bottom at the same time. The last card C is moved to the fifth position of a six-card list. This is exactly the surving position:

Based on the rule of odd-in and even-out in the Josephus problem, the other half C card is the last remaining card:

At last, the match was made!


Conclusion

Based on those two math problems, Liu elegantly designed his very interesting magic game. The first math is a list of replicated sequences. He could repeatedly move two matched cards to the head and tail of the list. Then keep the first card as a secret, to-be-matched card. The second is the Josephus problem. He continued his deceiving steps to move the last card to the right position and finally found the matched card.

With my above analysis, you may further design your own and more interesting steps. You may use various numbers of cards, such as 2, 3, 5, or even more.

After this game, do you have any other interesting math problems? You may define another totally different but also very interesting gamic game. Share yours with us!

References

Read More...

Wednesday, February 14, 2024

Liu Qian's Magic Card Show in TV Gala

Liu Qian presented a magic card show at the Chinese Spring Festival TV Gala 2024. Like most magic shows, being interactive with the audience is a very popular way to make the show interesting. Liu's show was so amazing and well received that even audiants in front of TV could participate and get unexpected and unblievable results.

I watched the show on YouTube. When I saw the amazing result, I could not wait any longer to give it a try. I did get a similar and amazing result. Initially, I could not find play cards at that time, and I did not want to destroy cards simply by playing the game. I wanted to do the show with my grandson. I asked him to get a piece of paper to make cards with four different numbers and any drawing shapes. We enjoyed the game very much.

I tried the game several times. Afterwards, I got my understanding and some ideas about the game. I made some modifications to replace Chinese contents with Western ways. Finally, I created the following modified version of the game.

This game is related to mathematics and computer science. Therefore, I write this on my programming blog.

Modifications of the Rules

In Alberta, we have two hokey players: the Flames in the south city of Calgary and the Oilers in the north city of Edmonton. I'll use them as players. The following are the rules and related modifications:

  • In the original show, people from the south had number one and those from the north had number two.
    I made this change: Flames at the sourth have number one, and Oilers on the north have number two.
  • The number of Chinese characters in one's name was used in the show.
    The number of Flames and Oilers names is 6. Another alternative for fun is to use the number of vowels or consonants, just for kits to learn.
  • One interesting choice is the sex of a random person: male as number one and female as number two in the show.
    I use the flip of a coin, the head as number one, and the tail as number two.
  • The secret or magic words are 見-證-奇-蹟-的-時-刻
    I use the English word m-a-g-i-c-a-l

Based on my understanding and analysis, I categorise all the steps in the game into three groups.

Build Cards' Structure - Preparation 1

Those are steps to build cards in the right structure.

  1. Fully shuffle four cards first. Fold the cards in half. Then, based on the fold line, rip the cards into two halves. Finally, put one half on top of another half. The total number of cards is eight.
  2. I chose the Flames to start a game. I read the name of the characters one by one, and at the same time, I place the top card on the bottom of the deck. In this way, I repeat the process six times.
  3. Take the top three cards up and place them any place between the remaining cards, i.e., after the first, second,... or fourth card (before the fifth). I put the top three cards after the fourth card.
  4. Pick up the top card. Keep it in a secret or safe place. This is the secret card.

Up to this step, you have seven half-cards in your hand. Eventually, you will have a last-half card left, and the card will match the secret card, which is the other half.

Deceive or Confuse - Preparation 2

Continue to interact with audiences. The steps in this second group would further deceive or confuse them. By the time of the last step, the right number of cards will be ready for the steps in the last group.

  1. I chose the Flames team, which is in the south of our province. Based on the above rule, the number is one. Take the top card from the deck and place it between the remaining cards. I place the top card behind the third card. It could be placed behind the first card, the second card,..., or the fifth one.
  2. At this step in Liu Qian's show, Liu used male or female for a random choice. Based on my above modifications, the alternative random choice is to throw a coin. My grandson received some chocolate candy coins. We made an agreement: one side as number one, another side as number 2. I explained to him that this is like a hokey game to choose the starting side in the beginning. He threw the coin and got the number two. I asked him to take the top two cards and throw them into the air.
    I asked my grandson to mimic Liu and Xiao Ni (one famous show anchor as the game player). They said, Sorry, cleaning maids. My grandson said, Sorry, dady and mami, we will clean those cards.

Magic Words for Magic Results

Up to this step, there are only five half-cards left. The game entered the most interesting and exciting stage.

  1. Read the magic words in the above rules, one character by one. For each read, place the top card at the bottom. The order is as follows: m-a-g-i-c-a-l.
  2. The last step is the moment in the most emotional and exciting mode. Place the top card at the bottom. At the same time, say it loudly: Keep my luck. Then take the top card from the remaining deck. Throw the card into the air and say, Announcing things, go away!.
    Repeat this sequence until the last card is in the hand.

Now get the secret card and place it next to the last one. Turn them over. What magic!

The last half-card left in your hand is exactly the other half-card, the secret card!

Liu Qian played such an extraordinary magic show at the Chinese Spring Festival Gala. Almost all the audiences, including me and my grandson, in front of the TV, proved this magic result.

Liu said that this is a very ancient magic game. Every year on the eve. of the New Year, many people play this magic game. If you succeed, your days in the next year will be full of happiness; if you fail, then repeat watching and playing the game till you win.

Summary

Liu Qian presented a really good magic game. After following the show and seeing the result, I could not explain the logic, which is really magic. Anyone could play this game, such as alduts. However, I think this game would be very attractive to young kids. The steps in the game are very complicated and long. It is hard to memorise those steps. I repeatedly watch the show, and I think it will be forgotten after a while. That's why I wrote this blog with all the steps. Those steps are grouped into three categories. I think this will be helpful as a study place and reference.

Liu's show was in Chinese. In order to help non-Chinese-speaking people as well as oversee Chinese kids, I modified some parts of the game, based on my understanding, with other alternatives, such as creating cards from a piece of paper, drawing characters, numbers, or shapes, using sports teams, throwing a coin, counting characters, vowels, and consonants, using different magic words, et cetera. In this way, it might attract more people around the world to enjoy the game.

Although the steps in this game, including your modifications, are very long and complicated, those are the magic spirits that make it very hard to grasp its logic. They provide you with an opportunity to demonstrate or practice your show-time talent and bring magic and happiness to everyone. Like many new things, the process of learning and practicing is very important. The repetition will help you to link steps in a fluent and natural way, and the show-time will help you to control rhythm and to present your confidence.

With some changes I made to Liu's show, I hope you can create your version better than mine. Enjoy the game!

References

Read More...

Sunday, January 28, 2024

File Diff

Today I found a very good article about how to compare two text files. This is a handy tool not only for developers, but also good for people who want to quickly find out diference between files.

In the article, it mentioned 2 ways to access this tool. Actually, there is another very handy method to launch the app: Spotlight Search. Just press Cmd+space, then type FileMerge.

The article explains well about the app's usage. Read the article.

References

Read More...