如何添加UIGestureRecognizer来识别UIImageView顶部的左或右滑动?我只需要在滑动时调用一个动作,另一个动作用于滑动是正确的.
我有一个UIView
和我使用下面的代码来实现它.
[myView setBackgroundColor:[UIColor redColor]];
[[myView layer] setCornerRadius:[myView bounds].size.height / 2.0f];
[[myView layer] setMasksToBounds:YES];
[myView setClipsToBounds:YES];
Run Code Online (Sandbox Code Playgroud)
然后添加 UIPanGestureRecognizer
移动框
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(boxIsMoving:)];
[myView addGestureRecognizer:panGesture];
Run Code Online (Sandbox Code Playgroud)
但问题是当用户在该轮之外点击但在实际框架中并开始拖动我的视图也开始移动.任何人都可以建议我怎么能忽略这一轮之外的接触.
objective-c rounded-corners uigesturerecognizer ios uipangesturerecognizer
我有一个视图(A),其中包含几个矩形子视图(B)。这些子视图中的每一个都有一个点击识别器来触发一个动作。父视图 A 也有一个单击识别器,它调用 A 控制器上的一个函数,使每个子视图 B 以一种颜色闪烁。这是函数:
@IBAction func highlightAreas(recognizer: UITapGestureRecognizer) {
for area in buttons {
// only show linked areas
if area.targetPage != nil {
let oldColor = area.backgroundColor
// show areas with animation
UIView.animateWithDuration(HIGHLIGHT_ANIMATION_TIME, animations: { Void in // begin of closure
area.backgroundColor = self.HIGHLIGHT_BACKGROUND_COLOR.colorWithAlphaComponent(self.HIGHLIGHT_ALPHA)
}) // end of closure
// hide areas with animation
UIView.animateWithDuration(HIGHLIGHT_ANIMATION_TIME, animations: { Void in // begin of closure
area.backgroundColor = oldColor?.colorWithAlphaComponent(0.0)
}) // end of closure
}
}
}
Run Code Online (Sandbox Code Playgroud)
它有效,但在动画期间子视图 B …
当我的图像被点击时,我有一个简单的代码块来播放声音.但是,当我点按图像时,甚至无法识别水龙头.
我相信这是真的,因为handleTap函数中的println在点击图像时不会打印任何内容.
谁能给我一些关于问题可能在哪里的见解?
var coinSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("wavSampleSound", ofType: "wav")!)
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
loadSound()
let gesture = UITapGestureRecognizer(target: self, action: "handleTap:")
gesture.delegate = self
myImage.addGestureRecognizer(gesture)
}
func loadSound() {
audioPlayer = AVAudioPlayer(contentsOfURL: coinSound, error: nil)
audioPlayer.prepareToPlay()
}
func handleTap(gesture: UITapGestureRecognizer) {
let playsSound = self.audioPlayer.play()
println("\(playsSound)")
}
@IBOutlet weak var myImage: UIImageView!
Run Code Online (Sandbox Code Playgroud)
}
我正在学习 swift(和 spritekit)并尝试制作一个简单的游戏。
在我的游戏中,英雄应该跳跃或躲避......
英雄需要在点击屏幕时跳跃,或者如果点击屏幕+按住(long gesture
)
所以基本的伪代码:
if tapped
heroJump()
else if tappedAndHeld
heroDuck()
Run Code Online (Sandbox Code Playgroud)
我有一个func
在几乎所有教程中都可以看到的,它处理触摸事件:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self) //need location of tap for something
switch gameState {
case .Play:
//do in game stuff
break
case .GameOver:
//opps game ended
}
break
}
}
super.touchesBegan(touches, withEvent: event)
}
Run Code Online (Sandbox Code Playgroud)
有没有办法,包括在这个触摸事件中,决定它是被点击还是被按住?我似乎无法理解这个事实,程序总是会在长手势之前识别出点击?!?
无论如何,为了解决我的问题,我发现了这个问题,它向我介绍了识别器,我试图实现它:
override func didMoveToView(view: SKView) {
// other stuff
//add long press …
Run Code Online (Sandbox Code Playgroud) 我正在尝试制作由 a 触发的自定义交互式过渡,UISwipeGestureRecognizer
但状态始终为UIGestureRecognizerStateEnded
,因此我无法使用它来控制动画。
Apple 的文档说Some of these states are not applicable to discrete gestures
但他们没有说哪些状态可用于UISwipeGestureRecognizer
. [ https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/index.html#//apple_ref/occ/instp/UIGestureRecognizer/state]
难道UISwipeGestureRecognizer
只能有一个状态还是我做错了什么?
我有一个带滑动手势的UIView.
let swipeUpGesture = UISwipeGestureRecognizer(target: self, action: #selector(NextStepCaptureVC.handleSwipeUp(gesture:)))
swipeUpGesture.direction = .up
view.addGestureRecognizer(swipeUpGesture)
func handleSwipeUp(gesture: UISwipeGestureRecognizer) {
print("Swipe Up")
heightSlider.setValue(20, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
当我尝试更改它的值时,但值从0跳到20.我希望值在刷卡时连续变化.我该怎么做?
我需要将UIGestureRecognizer添加到WKWebView.
我将点击手势设置为WKWebView,它一直有效,直到web视图加载url.加载网址后,它无法识别手势.
有什么办法可以为WKWebView添加手势吗?
import UIKit
import WebKit
class SwipeObserveExperimentController: UIViewController {
let webView: WKWebView = {
let v = WKWebView()
v.translatesAutoresizingMaskIntoConstraints = false
v.scrollView.backgroundColor = .green
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
// Layout
view.addSubview( webView )
webView.translatesAutoresizingMaskIntoConstraints = false
webView.topAnchor .constraint(equalTo: view.topAnchor ).isActive = true
webView.bottomAnchor.constraint(equalTo: view.bottomAnchor ).isActive = true
webView.rightAnchor .constraint(equalTo: view.rightAnchor ).isActive = true
webView.leftAnchor .constraint(equalTo: view.leftAnchor ).isActive = true
// Gesture
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(viewTap) )
webView.addGestureRecognizer(tapGesture)
// Load URL
let …
Run Code Online (Sandbox Code Playgroud) 具有以下视图结构:
以编程方式将点击手势 rec 添加到 Temp lbl:
let tempLblTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MainFeedVC.convertDegrees))
tempLblTap.delegate = self
tempLblTap.numberOfTapsRequired = 1
tempLblTap.numberOfTouchesRequired = 1
tempLblTap.cancelsTouchesInView = false
self.tempLbl.isUserInteractionEnabled = true
self.tempLbl.addGestureRecognizer(tempLblTap)
Run Code Online (Sandbox Code Playgroud)
但该方法convertDegrees
未触发。
还有 2 个滑动手势识别器添加到同一视图中:
let leftSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showPostPicVC))
leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(leftSwipeGestureRecognizer)
let rightSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showUserVC))
rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(rightSwipeGestureRecognizer)
Run Code Online (Sandbox Code Playgroud)
也许他们是原因?
我做了一个简单的项目,带有滑动手势识别器和动画。我让我的标签移动,每 3 秒增加一次。每次滑动我都需要减少数字。我的手势识别器对象与标签绑定,即它仅适用于标签边界。当 prog 在没有动画的情况下工作时一切正常,但是当它被动画并移动时,我的手势识别器什么都不做。如何使手势识别器与动画同时工作,即在动画响应我的滑动时。需要帮忙。
`
@IBOutlet weak var label1: UILabel!
var number : Int = 0
var timer = Timer()
@IBAction func label1SwipeRight(_ sender: UISwipeGestureRecognizer) {
number += 1
label1.text = String(number)
}
func animate1() {
UIView.animate(withDuration: 4.0, delay: 0.0, options: .allowUserInteraction, animations: {
let num1 : CGFloat = CGFloat(arc4random_uniform(667))
let num2 : CGFloat = CGFloat(arc4random_uniform(375))
self.label1.frame.origin.y = num1
self.label1.frame.origin.x = num2
}, completion: {(bool) in
self.animate1()
print("Animation1 completed")
})
}
func timerExample() {
Timer.scheduledTimer(timeInterval: 4, target: self, selector: …
Run Code Online (Sandbox Code Playgroud) ios ×8
swift ×7
iphone ×2
objective-c ×2
animation ×1
cocoa-touch ×1
ios9 ×1
sprite-kit ×1
uiimageview ×1
uislider ×1
uiview ×1
wkwebview ×1
xcode ×1