在我的viewDidLoad函数中,我有一个循环创建一个3x6的方形图像(块)表。我们的想法是最终移动并匹配它们。
我的问题是,如何将当前正在触摸或拖动的图像指定为变量?这是我添加图片的方式:
let squareImg = UIImageView(image:#imageLiteral(resourceName: "square"))
view.addSubview(squareImg)
以下是我正在尝试做的事情:
@IBAction func handlePan(_ recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if recognizer.state == UIGestureRecognizerState.began {
isDragging = true
let objectDragging = self.view //this is wrong and what I'm trying to correct
print("bg color of block being moved \(objectDragging?.backgroundColor)")
....
答案 0 :(得分:0)
试试这个
<body style="background-color: #eee">
<div class='head_msg'></div>
<div class="msg_user_area">
<br><br><br>
<?php
echo "Your conversations";
$my_id = $_SESSION['log_id'];
$gsql = <<<EOF
SELECT hash, user_one, user_two FROM connect WHERE user_one = '$my_id' OR user_two = '$my_id';
EOF;
$gret = $db->query($gsql);
while ($grow = $gret->fetchArray(SQLITE3_ASSOC)) {
$hash = $grow['hash'];
$user_one = $grow['user_one'];
$user_two = $grow['user_two'];
if ($user_one == $my_id) {
$select_id = $user_two;
} else {
$select_id = $user_one;
}
$sesql = <<<EOF
SELECT * FROM User WHERE ID = '$select_id';
EOF;
$seret = $db->query($sesql);
while ($serow = $seret->fetchArray(SQLITE3_ASSOC)) {
$select_uname = $serow['fname'];
echo "<a href='user_msg.php?hash=$hash'><div class='text'>$select_uname</div></a>
}
}
?>
</div>
<div class='padder'>
<div class='msg_area'>
<?php
if (isset($_GET['hash']) && !empty($_GET['hash']))
{
$hash = $_GET['hash'];
$us_id = $_SESSION['log_id'];
$mesql =<<<EOF
SELECT from_id, message FROM messager WHERE group_hash = '$hash';
EOF;
$meret = $db->query($mesql);
while ($merow = $meret->fetchArray(SQLITE3_ASSOC))
{
$from_id = $merow['from_id'];
$messages = $merow['message'];
$usql =<<<EOF
SELECT * FROM User WHERE ID = '$from_id';
EOF;
$uret = $db->query($usql);
while ($urow = $uret->fetchArray(SQLITE3_ASSOC))
{
$from_fname = $urow['fname'];
$from_img = $urow['image'];
if ($from_id != $_SESSION['log_id'])
{
echo "
<div class='from_bubble'><div class='from_img'><img src='$from_img'></div><div class='from_txt'><p>$messages</p></div></div>";
}
else
{
echo "
<div class='rep_bubble'><div class='rep_img'><img src='$from_img'></div><div class='rep_txt'><p>$messages</p></div></div>";
}
}
}
if (isset($_POST['rep_msg']) && !empty($_POST['rep_msg']))
{
$rep_msg = $_POST['rep_msg'];
$rsql =<<<EOF
INSERT INTO messager (message, group_hash, from_id) VALUES('$rep_msg', '$hash', '$my_id');
EOF;
$rret = $db->exec($rsql);
$ursql =<<<EOF
SELECT * FROM User WHERE ID = '$my_id';
EOF;
$urret = $db->query($ursql);
while ($urrow = $urret->fetchArray(SQLITE3_ASSOC))
{
$from_fname = $urrow['fname'];
$from_img = $urrow['image'];
header('Location: user_msg.php?hash='.$hash);
}
}
}
?>
</div>
<div class="rep_area">
<form action="" id="send_rep" method="post" enctype="multipart/form-data">
<div class="textarea"><textarea name="rep_msg" placeholder="Message here"></textarea></div>
<input type="submit" value="Send">
</form>
</div>
</div>
<script type="text/javascript">
$(document).ready(function (){
var msg_area = $('.msg_area');
msg_area.scrollTop(msg_area.prop("scrollHeight"));
//Tried this but didn't work
$('#send_rep').submit(function (e) {
e.preventDefault();
$.ajax({
url: 'user_msg.php',
type: 'POST',
data: $(this).serialize(),
dataType: 'html'
})
.done(function (data) {
$('.from_bubble').fadeIn('slow').html(data);
});
});
});
</script>
答案 1 :(得分:0)
您可以使用UIImageView
提供的替代。
例如,
class DragImag: UIImageView {
var draggedView: UIView?
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?){
//here you get your desired view
draggedView = touches.view
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?){
}
}
答案 2 :(得分:0)
将图像存储在数组中,将其索引分配给标记值,使用touchesBagan等进行操作,然后只使用手势识别器。
添加图片变为:
var myImages = [UIImage]()
let squareImg = UIImageView(image:#imageLiteral(resourceName: "square"), tag: [//add a distinguished tag here])
view.addSubview(squareImg)
myImages.append(squareImg)
现在您的视图位于一个数组中(它们不需要是UIImageViews甚至是UIImages,只是一些独特的东西),只需指向数组中正确的视图:
@IBAction func handlePan(_ recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
for view in self.subviews as [UIView] {
if let imageView = view as? UIImageView {
let image = myImages.tag
}
}
}
我知道这并不是你要求的。但技术是。我会根据您的反馈调整我的答案。
编辑:
根据评论,听起来好的策略是至少用某种循环中的&#34;关联对填充标记属性。此外,它听起来像某种构建错误正在发生。这是我对这个问题的看法....
标记属性可用于每个对象,无论是视图还是控件。此外,它们属于Int类型。最后一条注释表明错误在函数调用中引用了一个不正确的参数,期望一个图像。我认为有两个不同的事情正在发生。
(1)填充&#34;关联&#34;的标签属性循环中的图像。
如果您要加载&#34; square&#34;和一个&#34;圈&#34; n次,用索引在循环中实例化事物并填充其标签属性。
for index in 0...10 {
// instantiate your images
// associate these images by populating their tag values
imgSquare.tag = index
imgCircle.tag = index
// continue any other set here
}
Swift中的循环类型本身就是Int,所以这是正确的。
(2)错误的参考构建错误。
构建错误在线
let circleImg = UIImageView(image: circle, tag: [arrayCounter])
这不是&#34;罐头&#34; UIImageView的初始化程序,并且从构建错误中它还没有(还)UIImageView的编码扩展。听起来 是UIImageView的编码扩展,需要两个参数, image 和 highlightImage 。如果你想扩展UIImageView这样的东西,我会用一个方便的初始化器创建一个扩展:
extension UIImageView {
convenience init(image: UIImage, tag:Int) {
self.init()
self.image = image
self.tag = tag
}
}
这是一个骨架。您可以添加一个保护语句并使其成为可用的初始化程序。你可以添加帧,backgroundColor,等等。问题是 - 你现在不有一个初始化程序要求&#39; image:tag:&#39;但是你正在编写一个。