How to get a CGPoint from a tapped location?
you have two way ...
1.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
}
here,you can get location with point from current view...
2.
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapRecognizer];
here,this code use when you want to do somthing with your perticular object or subview of your mainview
how to get tap point (coordinates) in iOS swift
In PageViewController,if you want to get pop up
In viewDidLoad:
let tap = UITapGestureRecognizer(target: self, action: "showMoreActions:")
tap.numberOfTapsRequired = 1
view.addGestureRecognizer(tap)
Make the page view controller inherit UIGestureRecognizerDelegate
then add:
func showMoreActions(touch: UITapGestureRecognizer) {
let touchPoint = touch.locationInView(self.view)
let DynamicView = UIView(frame: CGRectMake(touchPoint.x, touchPoint.y, 100, 100))
DynamicView.backgroundColor=UIColor.greenColor()
DynamicView.layer.cornerRadius=25
DynamicView.layer.borderWidth=2
self.view.addSubview(DynamicView)
}
UIPageViewController with TouchEvent
How could I get the CGPoint of the TapGesture in SwiftUI
Here is possible approach (tested with Xcode 11.4 / iOS 13.4)
fileprivate struct ImageWrapper: View {
let image: UIImage
@State var lastLocation: CGPoint = .zero
var body: some View {
let fitToFill = TapGesture(count: 2)
.onEnded {
// << use self.lastLocation here as needed
}.simultaneously(with:
DragGesture(minimumDistance: 0, coordinateSpace: .local).onChanged { value in
// location in image own coordinates, if needed in screen,
// then change above coordinateSpace to .global
self.lastLocation = value.location
})
return Image(uiImage: image)
.renderingMode(.original)
.gesture(fitToFill)
}
}
Getting the coordinates from the location I touch the touchscreen
In a UIResponder
subclass, such as UIView
:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject()! as UITouch
let location = touch.locationInView(self)
}
This will return a CGPoint
in view coordinates.
Updated with Swift 3 syntax
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.first!
let location = touch.location(in: self)
}
Updated with Swift 4 syntax
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
let location = touch.location(in: self.view)
}
Find CGPoint Location of substring in TextView
You can use firstRectForRange(_:)
method on UITextView
let textFont = [NSFontAttributeName: UIFont(name: "GillSansMT", size: 30.0) ?? UIFont.systemFontOfSize(18.0)]
let attrString1 = NSMutableAttributedString(string: "My name is Dug.", attributes: textFont)
// range of substring to search
let str1 = attrString1.string as NSString
let range = str1.rangeOfString("name", options: nil, range: NSMakeRange(0, str1.length))
// prepare the textview
let textView = UITextView(frame:CGRectMake(0,0,200,200))
textView.attributedText = attrString1
// you should ensure layout
textView.layoutManager.ensureLayoutForTextContainer(textView.textContainer)
// text position of the range.location
let start = textView.positionFromPosition(textView.beginningOfDocument, offset: range.location)!
// text position of the end of the range
let end = textView.positionFromPosition(start, offset: range.length)!
// text range of the range
let tRange = textView.textRangeFromPosition(start, toPosition: end)
// here it is!
let rect = textView.firstRectForRange(tRange)
How to transform multiple view to same location(CGPoint)
I think your problem was that you didn't reduce the size of the image view so that they stack at the exact centre of the screen.
let deltaX = (self.view.center.x - card.frame.minX) - card.frame.width/2
let deltaY = (self.view.center.y - card.frame.minY) - card.frame.height/2
UIView.animate(withDuration: 1) {
card.transform = .init(translationX: deltaX, y: deltaY)
}
Related Topics
Unexpected Non-Void Return Value in Void Function Swift3
Core Data in Swift: Only Saving Last Object in a for Loop
Ios: Pre Install Ssl Certificate in Keychain - Programmatically
Error: Error Domain=Nsurlerrordomain Code=-1001 "The Request Timed Out."
Nsattributedstring Superscript Styling
Swift Version of Componentsseparatedbystring
Avcapturesession and Background Audio iOS 7
Get Pixel Data as Array from Uiimage/Cgimage in Swift
How to Run Nstimer in Background Beyond 180Sec in iOS 7
iOS 13.1 Crash in Avaudio Player
Ios: Frame.Size.Width/2 Doesn't Produce a Circle on Every Device
Firebase with Swift 3 Counting the Number of Children
Uncaught Exception: This Class Is Not Key Value Coding-Compliant
Dismiss View Controller with Custom Animation
Cannot Subscript a Value of Type '[Nsobject:Anyobject]' with an Index of Type 'String'