Detect UIImageView Touch in Swift
You can put an UITapGestureRecognizer
inside your UIImageView
using Interface Builder or in code (as you want), I prefer the first. Then you can put an @IBAction
and handle the tap inside your UIImageView
, Don't forget to set the UserInteractionEnabled
to true
in Interface Builder or in code.
@IBAction func imageTapped(sender: AnyObject) {
println("Image Tapped.")
}
I hope this help you.
Detect Touch Event On UIScrollView's Content
Your UIImageView
s probably have user interaction disabled (which is what they default to). Call the following line on each image view, and your taps should be recognized:
yourImageView.userInteractionEnabled = YES;
Make sure to do this before adding the gesture recognizer.
I had this exact issue a few weeks ago and this is what fixed it for me.
How to identify a button touch event from other touch events
To know whether UIButton is pressed follow this:
-(void) touchBegan : (NSSet *) touches withEvent : (UIEvent *) even
{
UITouch *touch = [touched anyObject];
UIView *touchedView = [touch view];
if([touchedView isMemberofClass : [UIButton class])
{
//do something when button is touched
}
}
How to recognize a touch event on UIImageView while animating in ios using objective c code?
Add this method & load this method on viewDidLoad
instead of loading SwipeImages
on viewDidLoad
-(void)addTimer
{
aTimer=[NSTimer scheduledTimerWithTimeInterval:15
target:self
selector:@selector(SwipeImages)
userInfo:nil
repeats:YES];
}
Then your viewDidLoad looks like
scrlNo = 0;
imgslide.userInteractionEnabled = YES;
[self addGestures];
[self addTimer];
And Edit SwipeImages to this,
- (void)SwipeImages
{
if (scrlNo==1) {
imgslide.image = [UIImage imageNamed:@"promo_banner_2.png"];
scrlNo=2;
}else if (scrlNo==0) {
imgslide.image = [UIImage imageNamed:@"promo_banner_1.png"];
scrlNo=1;
}
else if (scrlNo==2) {
imgslide.image = [UIImage imageNamed:@"promo_banner_3.png"];
scrlNo=3;
}
else if (scrlNo==3) {
imgslide.image = [UIImage imageNamed:@"promo_banner_7.png"];
scrlNo=0;
}
else if (scrlNo==4) {
}
}
How to get touch location when clicking a UIImageView?
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: imageView)
print(position.x)
print(position.y)
}
}
Related Topics
In iPhone App How to Detect the Screen Resolution of the Device
Select Multiple Images (Uiimagepickercontroller or Photos.App Share Ui)
Iphone: Hide Uitableview Search Bar by Default
How to Create a Scroll View with a Page Control Using Swift
Xcode 4.5 and iOS 4.2.1 Incompatibility
Retrieve Custom Prototype Cell Height from Storyboard
How to Create a Category in Xcode 6 or Higher
Uicollectionview Reloaddata Not Functioning Properly in iOS 7
Need Assistance Setting Two Buttons of Equal Width, Side by Side Autolayout
How to Get an Iso 8601 Date on iOS
Nspersistentcontainer Concurrency for Saving to Core Data
Uitextview That Expands to Text Using Auto Layout
Swift 2: Call Can Throw, But It Is Not Marked with 'Try' and the Error Is Not Handled