簡體   English   中英

如何在Swift中比較兩點

[英]How to compare two points in Swift

我有一個包含文本和圖像的UITextView 我也有一個CGPoint數組,其中包含用戶在UITextFied添加的圖像的坐標。 每當UITextView文本發生更改(添加或刪除)時,我都會獲得CGPoint對象的光標位置。

現在,我想遍歷CGPoint數組以查找在光標位置之后的同一行或下面的行中有多少圖像。 我該怎么辦?

任何幫助將不勝感激。

當然:

var cursor = message.caretRectForPosition(message.selectedTextRange?.end).origin;    
for i in 0...emoji1.count-1 {
        if ((cursor.x > emoji_pt[i].x)  && (cursor.y <= emoji_pt[i].y)) {
            continue;
        }
        else {
            //the emoji is after the cursor.
            // Return the index and process all images after that index
        }
    }

處理Y位置

Bool onPreviousLine = (emoji_pt[i].y < cursor.y)

要處理X位置( lineHeight是一個常量,具體取決於您的大小寫和圖像的大小,您可能會lineHeight一些較低的常量值(例如1.0 ))。

Bool onTheSameLine = abs(Double(emoji_pt[i].y - cursor.y)) < Double(lineHeight / 2)
Bool beforeX = (emoji_pt[i].x < cursor.x)

一起

if onPreviousLine || (onTheSameLine && beforeX) {
    continue
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM